我正在学习pHp。我已经做了一个登录页面。我在这里面临的问题是,当用户单击登录按钮并找到记录时,他将被带到显示重定向链接的其他页面,用户必须单击该链接才能进入下一页。现在我想要的是,当用户单击登录按钮时,应该在数据库中对详细信息进行交叉检查,如果找到了记录,则应将用户直接重定向到下一页,否则应该显示错误。这是我的html页面:
<!DOCTYPE html>
<html>
<head>
<title>OpenMoz</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="index.css"/>
</head>
<body style="height:650px;">
<h1 align="center" ><b><i>City Login</i></b></h1>
<div class="login">
<form action="login.php" method="post">
<input type="text" placeholder="Username" name="username" autocorrect=off autocapitalize=words required> <br>
<input type="password" placeholder="password" name="password" autocorrect=off autocapitalize=words required> <br>
<a href="home.php"><input type="submit" name="submit" value="Sign In"></a>
</form>
<a href="signup.php"><input type="submit" name="submit" value="Sign Up"></a>
<div>
</body>
</html>
这是用于验证详细信息的login.php脚本:
<?php
$username = $_POST["username"];
$password = $_POST["password"];
if($username && $password)
{
$connect = mysql_connect("localhost","root","password") or die("Couldn't connect");
mysql_select_db("phplogin")or die("Couldn't connect");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if($numrows!=0)
{
while($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if($username==$dbusername && $password==$dbpassword)
{
echo ("<center><a href='home.php'>Redirect</a></center>");
$_SESSION['username'] = $username;
}
else
{
echo ("Incorrect Password !");
}
}
else
die("The user doesn't exist");
}
else
echo ("Please enter username & password");
?>
如果我的问题得到解决,我将非常感激。
只要您没有向浏览器输出任何内容,就可以执行标头重定向。这将实现您的目标。
更改此:
echo ("<center><a href='home.php'>Redirect</a></center>");
$_SESSION['username'] = $username;
对此:
$_SESSION['username'] = $username;
header("Location: /some-new-page.php");
exit;
始终exit;
在位置重定向之后。
哦,是的,请清理您的输入...您对SQL注入很开放。
$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string($_POST["password"]);
哦,是的。.和mysql_ *已过时。使用mysqli_ *
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句