我创建了登录会话,它显示了一个错误:在这些行中有未标识的myusernamemypassword。我不明白为什么。

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

这是我的checklogin.php
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

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

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

这是我给洛金的表格:
<form action='checklogin.php' method="post" >
Username
<input name='myusername' type="text">
Password
<input name='mypassword' type="password">

<input type="submit" name="Submit" value="Login">

</form>

最佳答案

使用前检查$_POST['myusername']$_POST['mypassword']是否已设置

if(isset($_POST['myusername']) && isset($_POST['mypassword']) && !empty(trim($_POST['myusername'])) && !empty(trim($_POST['mypassword']))) {

 }

它的形式动作看起来应该是login_true.php
<form action='checklogin.php' method="post" >

因为您是在checklogin.php中发送数据,而不是在login_true.php中,所以您得到错误:不明bla bla
警告:您的代码对sql injunction完全打开
读得好:
Best way to prevent SQL injection?
PDO Tutorial for MySQL Developers
Pdo for beginners ( why ? and how ?)

关于php - 登录 session php,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13599035/

10-17 02:11