我是PHP代码的新手,如果用户输入了错误的用户标识或密码,我想在login.php中显示错误信息.我写了两页代码.login.php页面将用户名和密码提交给check.php页面.如果用户名和密码正确,则重定向到xyz.php else到登录页面.
login.php //登录页面
<form name="login" enctype="multipart/form-data" action="checkpage.php" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Username</th>
<td><input type="text" name="username" value="sandeep" onfocus="this.value=''" class="login-inp" /></td>
</tr>
<tr>
<th>Password</th>
<td><input type="password" name="password" class="login-inp" /></td>
</tr>
<tr>
<th></th>
<td valign="top"><input type="checkbox" class="checkbox-size" id="login-check" /><label for="login-check">Remember me</label></td>
</tr>
<tr>
<th></th>
<td><input type="submit" class="submit-login" /></td>
</tr>
<tr><th></th><td>w want to display error message here..</td></tr>
</table></form>
Run Code Online (Sandbox Code Playgroud)
checkpage.php //连接页面
<?php
session_start();
//connecting to db
$username=$_POST['username'];
$pwd=$_POST['password'];
$q="select *from xyz where username='$username' AND password='$pwd'";
$qry=mysql_query($q);
if(mysql_num_rows($qry)>0)
{
$_SESSION['username']=$username;
echo "<script>window.open('xyz.php','_self')</script>";
}
else{
header("location:login.php");
}
?>
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常但我想显示错误消息我怎么能显示错误消息.请指导我.
您可以在check.php脚本中将消息设置为session,然后在获取它后立即取消设置(AKA"read once"):
$_SESSION['message'] = 'Your message';
Run Code Online (Sandbox Code Playgroud)
然后(在login.php中,您希望显示消息的位置):
if (isset($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}
Run Code Online (Sandbox Code Playgroud)