0

我在网上找到的 PHP 登录有一些问题 -

 <?php

$host="localhost"; // Host name 
$username="SOME_USERNAME"; // Mysql username 
$password="SOME_PASSWORD"; // Mysql password 
$db_name="b00556019"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
 }
else {
echo "Wrong Username or Password";
}
 ?>

错误在第 16-17 行,显示如下 -

Notice: Undefined index: myusername in E:\home\students\2137\B00556019\public_html\workspace\login\checklogin.php on line 16

Notice: Undefined index: mypassword in E:\home\students\2137\B00556019\public_html\workspace\login\checklogin.php on line 17

错误的用户名或密码

这是 2 行不工作:

 $myusername=$_POST['myusername']; 
 $mypassword=$_POST['mypassword']; 

这是HTML

<!DOCTYPE HTML>
<html>
<head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>#

任何有任何想法或更好的教程示例的人将不胜感激:)

4

3 回答 3

0

请用

if(isset($_POST['myusername']) && $_POST['myusername']!=''){
    $myusername=$_POST['myusername']; 
   $mypassword=$_POST['mypassword']; 

    // To protect MySQL injection (more detail about MySQL injection)
   $myusername = stripslashes($myusername);
   $mypassword = stripslashes($mypassword);
   $myusername = mysql_real_escape_string($myusername);
   $mypassword = mysql_real_escape_string($mypassword);
   $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and     password='$mypassword'";
  $result=mysql_query($sql);

  // Mysql_num_row is counting table row
  $count=mysql_num_rows($result);

 // If result matched $myusername and $mypassword, table row must be 1 row
 if($count==1){

 // Register $myusername, $mypassword and redirect to file "login_success.php"
   session_register("myusername");
   session_register("mypassword"); 
  header("location:login_success.php");
 }
else {
 echo "Wrong Username or Password";
}


}

这将解决您的问题

于 2014-03-12T14:26:43.990 回答
0

$_POST[]包含来自 HTML 表单提交的数据。如果用户没有在表单中发送,则该$myusername字段将为空,因为'myusername'在数组中找不到索引(实际上是键)。这就是您收到这些错误的原因。

// username and password sent from form 
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; 

要确保 POST 数据(来自表单提交)包含键/索引,请使用isset()命令,(或者array_key_exists(),两者都有效。)

if(isset($_POST['myusername']) && isset($_POST['mypassword']) {
    // username and password sent from form 
    $myusername=$_POST['myusername']; 
    $mypassword=$_POST['mypassword'];
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
        // Register $myusername, $mypassword and redirect to file "login_success.php"
        session_register("myusername");
        session_register("mypassword"); 
        header("location:login_success.php");
    }
    else {
        echo "Wrong Username or Password";
    } 
}

但是如果你想使用$_POST,你必须在你的 HTML 中编写一个表单。另外我建议不要使用表格来对齐 HTML 表单元素。这不是一个优雅的解决方案。有很多关于如何使用stackoverflow的示例。

下面是 HTML5 中的表单示例

<form action="#" method="post">
     <label for="myusername">Your username : </label>
     <input name="myusername" type="text" id="myusername" />
     <label for="mypassword">Your password : </label>
     <input name="mypassword" type="password" id="mypassword" />
     <input type="submit" name="Submit" value="Login">
</form>

<label for="input id">标签文本挂钩到输入框。如果用户单击文本,该for属性确保浏览器焦点将位于for指向的输入元素上。

那么<input>有很多属性。该name字段用于服务器端程序来处理表单提交。用于浏览器id本身。它主要由 CSS 和客户端脚本使用。

此外,您正在使用非常过时的教程来实现登录网页。mysql()功能已正式弃用。请PDO改用。

此外,本教程应该可以毫无问题地指导您进入 PDO 世界

于 2014-03-12T14:25:52.500 回答
0

添加表单开始标签,它将开始工作

<!DOCTYPE HTML>
<html>
<head>
<body>
<form action="" method="post">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"               bgcolor="#CCCCCC">
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr> 
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td> 
 </tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>

</form>
</body>

根据您的要求指定操作

于 2014-03-12T14:41:36.350 回答