我查看了许多网站,比较了各种登录脚本,但找不到任何解决方案。我输入了正确的信息,得到了一个"无效登录",如果我不输入任何信息,我就会得到一个"非法登录"。我真的需要一些帮助来解决问题。这是代码
<?php
include_once ('dbc.php');
$user_email = mysql_real_escape_string(isset($_POST['email']));
if (isset($_POST['Submit'])=='Login')
{
$md5pass = md5($_POST['pwd']);
$sql = "SELECT id,user_email FROM users WHERE
user_email = '$user_email' AND
user_pwd = '$md5pass' AND user_activated='1'";
$result = mysql_query($sql) or die (mysql_error());
$num = mysql_num_rows($result);
if ( $num = 0 )
{
// A matching row was found - the user is authenticated.
session_start();
list($user_id,$user_email) = mysql_fetch_row($result);
// this sets variables in the session
$_SESSION['user']= $user_email;
if (isset($_GET['ret']) && !empty($_GET['ret']))
{
header("Location: $_GET[ret]");
} else {
header("Location: myaccount.php");
}
//echo "Logged in...";
exit();
}
header("Location: login.php?msg=Invalid Login");
//echo "Error:";
exit;
}
?>
表单代码----
<td bgcolor="#e5ecf9" class="mnubody"><form name="form1" method="post" action="">
<p> </p>
<p align="center">Your Email:
<input name="email" type="text" id="email">
</p>
<p align="center"> Password:
<input name="pwd" type="password" id="pwd">
</p>
<p align="center">
<input type="submit" name="Submit" value="Login">
</p>
<p align="center"><a href="register.php">Register</a> | <a href="forgot.php">Forgot</a></p>
</form></td>
if (isset($_POST['Submit'])=='Login')
永远不会运行该块中的代码。isset返回true或false。
尝试
if (isset($_POST['Submit']) && $_POST['Submit'] =='Login')
这就是为什么您总是被重定向到无效的登录URL。除此之外,我并没有真正检查代码中是否存在任何逻辑错误。
此外,$user_email = mysql_real_escape_string(isset($_POST['email']));
也不会给你想要的结果。
参见isset()
您还需要将if ( $num = 0 )
更改为if ( $num > 0 )
。在任何一种情况下,都需要使用两个相等的==
来比较值。这将把0分配给$num,并执行if语句正文中的代码。
if($num=0)
应该是
如果($num==1)
http://php.net/manual/en/language.operators.comparison.php
更新:
您应该检查多个匹配的行。每当$num>1时抛出异常/日志。这样的检查将有助于在数据库成为完全灾难之前发现任何数据库一致性问题。