我一直在利用这个论坛来帮助创建我的第一个PHP网站。我已经完成了大部分工作,但是我遇到了登录问题。 如果新用户注册并登录到该网站,它可以正常工作,但是如果我打开登录数据库甚至一次,或者如果我在管理面板中查看用户列表,则以前的所有用户登录都会失败,没有人可以登录即使它们仍然存在于数据库中,也会再次使用这些凭据。有帮助吗? 以下是我的登录表单代码:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
</head>
<body>
<table border="1" width="100%">
<form action="user.php" method="post">
<tr>
<td><label for="u_name">Your Email-id:</label></td>
<td><input type="text"name="u_name" id="u_name" placeholder="Enter email-id" required="required"/></td>
</tr>
<tr>
<td><label for="u_password">Password:</label></td>
<td><input type="password" name="u_password" id="u_password" placeholder="Enter password" required="required"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="sub" value="login"/></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['sub']))
{
$u_name=$_POST['u_name'];
$u_password=$_POST['u_password'];
include ("connection.php");
$w="select * from signup";
$b=mysql_query($w) or die(mysql_error()."in query $w");
while($row=mysql_fetch_array($b))
{
$a=$row['email_id'];
$u=$row['password'];
}
if ($u_name==$a && $u_password==$u)
{
$_SESSION['loggedin']=true;
$_SESSION['user_email']=$_POST['u_name'];
$_SESSION['user_pass']=$_POST['u_password'];
echo '<meta http-equiv="refresh" content="0; url=homepage.php">';
}
else
{
echo "Your username or password is incorrect.Please try again";
}
}
?>
答案 0 :(得分:1)
<?php
if(isset($_POST['sub']))
{
$u_name=$_POST['u_name'];
$u_password=$_POST['u_password'];
include ("connection.php");
$w="select * from signup where email_id = '$u_name' AND password = '$u_password'";
$b=mysql_query($w) or die(mysql_error()."in query $w");
$num_rows = mysql_num_rows($b);
if($num_rows > 0)
{
$row=mysql_fetch_array($b);
$_SESSION['loggedin']=true;
$_SESSION['user_email']=$_POST['u_name'];
$_SESSION['user_pass']=$_POST['u_password'];
echo '<meta http-equiv="refresh" content="0; url=homepage.php">';
}
else
{
echo "Your username or password is incorrect.Please try again";
}
}
?>