如何在此代码中添加用户名?

时间:2016-01-13 07:02:11

标签: php

我是PHP的完全新手,我到目前为止的唯一原因是因为我试图猜测一切都是如此。我想我很幸运它到目前为止。

基本上,我正在使用我发现here的PHP Wake On LAN代码来创建一个我登录的简单页面,以远程打开我的计算机。我决定添加一个用户名,而不仅仅是一个密码,所以也许我可以拥有多个用户。到目前为止,我已经成功完成了一切,除了一件事。当我登录时,检查我的电脑,然后点击“全部选中唤醒”,它会立即将我注销,并且不会发送魔法包。关于我可以改变什么以解决这个问题的任何提示?一旦我删除if语句中的'和'和用户名位,检查用户和传递是否正确,它就可以工作。但是那时没有用户并通过验证继续进行。我正在谈论的if语句是第二个,如果在===测试密码保护下=== part。

您可以在我的网站上演示代码以查看此处的问题:http://trivisionzero.com/wol/ 只需使用'user','pass'即可。 (要重新创建它,选择任何计算机并按下唤醒按钮)

到目前为止的完整代码:

<center>
<br><br><br><br><br>
<?php
/* ============================== Configuration settings ====================================== */

/* List of PCs that may be woken */
$config_network_data_array[] = array("name" => "Shane-EPC", "MAC" => "changed for security", "IP" => "changed for security", "WakeIP" =>
"changed for security");
$config_network_data_array[] = array("name" => "Demo", "MAC" => "changed for security", "IP" => "changed for security", "WakeIP" =>
"changed for security");
$config_network_data_array[] = array("name" => "Demo", "MAC" => "changed for security", "IP" => "changed for security", "WakeIP" =>
"changed for security");

// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
$socket_number = "7";

$my_password = 'pass';
$my_username = 'user';

$html_title = '<H2>TrivisionZero PC Waker</H2>';

$config_table_columns = array('name', 'IP', 'MAC', 'links');

# The following function is copied (with some edits, to suppress output and return TRUE or an error message) from:
# http://www.hackernotcracker.com/2006-04/wol-wake-on-lan-tutorial-with-bonus-php-script.html

# Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
# Modified by Allan Barizo http://www.hackernotcracker.com

flush();

function WakeOnLan($addr, $mac,$socket_number) {

   $separator = ':';
   if (strstr ( $mac, '-' ) ) {
      $separator = '-';
   }
   $addr_byte = explode($separator, $mac);

   $hw_addr = '';
   for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
   $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
   for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
   // send it to the broadcast address using UDP
   // SQL_BROADCAST option isn't help!!
   $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
   if ($s == false) {
//      echo "Error creating socket!\n";
//      echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
      return "Error creating socket!\nError code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_erro
($s));
//      return FALSE;
      }
   else {
      // setting a broadcast option to socket:
      $opt_ret = socket_set_option($s, 1, 6, TRUE);
      if($opt_ret <0) {
//         echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
         return "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
//         return FALSE;
         }
      if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
//         echo "Magic Packet sent successfully!";
         socket_close($s);
         return TRUE;
         }
      else {
//         echo "Magic packet failed!";
         return "Magic packet failed!";
//         return FALSE;
         }
      }
   }

/* ============================== some predefined texts ====================================== */

$display_sent = 'Magic Packet sent successfully!';
$button_text = 'Wake!';
$button_text2 = 'Wake all selected';

//this is where I added my username part
$username_element = "<P>Username: <input type=\"text\" name=\"username\" />";
$password_element = "<P>Password: <input type=\"password\" name=\"password\" /><input type=\"submit\" name=\"submit\" value = \"Login\" />";


$table_html = "<TABLE border=\"2\">\n";
$logout_html = '';

/* ========================= Test for password protection ==================================== */
$wake_MAC_array = array();

if (!isset ($_POST['logout'])) {
   $input_password = $_POST['password'];
   $input_username = $_POST['username'];

}
//($my_password === '') is if you want no password
if (($input_password === $my_password) and ($input_username === $my_username)) {
   $logged_in = TRUE;
   $hidden_login = "<input type=\"hidden\" name=\"password\" value=\"$my_password\"/>";
   if ($my_password !== '') {
      $logout_html = "\n<P><input type=\"submit\" name=\"logout\" value=\"Log Out\"/>\n";
   }
   if ( (isset ($_POST['tickbox'])) and (is_array($_POST['tickbox']) ) ) {
      $checkbox_array = $_POST['tickbox'];
      foreach ($checkbox_array as $mac_address => $tickbox_setting) {
         $wake_MAC_array[$mac_address] = $tickbox_setting;
      }

   }

} else {
   $logged_in = FALSE;
   $hidden_login = '';
   $table_html_user = $username_element;
   $table_html = $password_element;
}

/* ================================ LOGGED-IN users only ===================================== */
/* ======================= construct table for listing of devices ============================ */

if ($logged_in == TRUE) {
   $table_row = "\n<TR>";
   foreach ($config_table_columns as $key => $column_heading) {
      $table_row .= '<TD>' . $column_heading . '</TD>';
   }
   $table_row .= '<TD>Wake Up!</TD>';
   $table_row .= '<TD>status</TD>';
   $table_html .= $table_row . "</TR>\n";
   foreach ($config_network_data_array as $device_key => $device_values) {
      $table_row = "\n<TR>";
      $mac = $device_values['MAC'];
      $device_name = $device_values['name'];
      $status_cell = '<TD>&nbsp;</TD>';
      foreach ($config_table_columns as $key => $column_heading) {
         if (isset ( $device_values[$column_heading])) {
            $value = $device_values[$column_heading];
            if ($column_heading == 'MAC') {
/* special coding for MAC address column; prepare clickable button */
               $this_MAC = $value;
               $value = "<input type=\"submit\" name=\"wake_MAC\" value = \"$value\" />";

               if (( $_POST['wake_MAC'] === $this_MAC ) or (array_key_exists ($this_MAC,
$wake_MAC_array))) {
                  $status = WakeOnLan ($device_values['WakeIP'], $this_MAC, $socket_number) ;
                  if ( $status === TRUE ) {
                     $status = $display_sent;
                  }
                  $status_cell = "<TD>$status</TD>";
               }
            }
         } elseif ($column_heading == 'links') {
/* special coding for links column; prepare clickable links from $config_network_links_array */
            $value = '';
            if (isset ( $config_network_links_array[$device_name])) {
               foreach ($config_network_links_array[$device_name] as $link_title => $link_URL) {
                  if ( $value !== '') {
                     $value .= '<BR />';
                  }
                  $value .= '<A HREF="' . $link_URL . '">' . $link_title . '</A>';
               }
            }
         } else {
            $value = '';
         }
      if ($value === '') {
         $value = '&nbsp;';
      }
      $table_row .= '<TD>' . $value . '</TD>';
      }
/* now add a checkbox to wake up this device */
      $table_row .= '<TD>' . "<input type=\"checkbox\" name=\"tickbox[$this_MAC]\" />" . '</TD>';
/* now add the status message (if applicable) for the attempt to send a packet to this device */
      $table_row .= $status_cell;
      $table_html .= $table_row . "</TR>\n";
   }
   $table_html .= "</TABLE>\n";
   $table_html .= "<P><input type=\"submit\" name=\"wake all\" value = \"$button_text2\" />\n";
}
/* =========================================================================================== */
/* ======================= Now output the html that we've built ============================== */

echo $html_title;

echo "<FORM name=\"input\" action=\"" .$_SERVER['PHP_SELF'] . "\" method=\"post\">";
echo '<P>';
echo $table_html_user;
echo $table_html;
echo $hidden_login;
echo $logout_html;
echo "</FORM>\n";

?>
</center>

1 个答案:

答案 0 :(得分:1)

当您登录然后提交表单以唤醒计算机时,您将在隐藏字段中传递密码,而不是用户名。

由于您没有传递用户名$input_username = null,因此支票if (($input_password === $my_password) and ($input_username === $my_username))变为if (('pass' === 'pass') and (null === 'user')),这是错误的,这就是您退出的原因。