我使用会话变量制作了一个登录表单。此登录表单是 index.php,此表单操作将在他继续之前将其重定向到 validate.php 页面。Vaidate.php 代码包含这个
<?php
session_start(); // Initialize session
include('config.php'); // Include database connection settings
$sql= mysql_query("select * from users where (username= '". mysql_real_escape_string($_post['uname'])."') and (password='".mysql_real_escape_string($_post['pass'])."')"); // Retrieve username and password from database according to user's input
// check if the username and password exists
if(mysql_num_rows($sql)==1)
{
// store the USERNAME in SESSION VARIABLE
$_SESSION['name'] = $_POST['uname'];
// and then JUMP to WELCOME page
header('Location:welcome.php');
}
else
{
// Jump to login page
//echo "<script type='javascript'>{alert('Username Or Password Incorrect')
// return false;
// }
header('Location:index.php');
}
?>
index.php 包含以下代码
<?php
session_start(); // function to start the session
if (isset($_SESSION['name'])) // Check, if user is already login, then jump to Welcome page
{
header('Location: welcome.php');
}
?>
</head>
<title>Login</title>
<body>
<form action="validate.php" method="post" name="log"/>
<h3 align="center" style="margin-top:40px">User Login</h3>
<table border="1" cellpadding="3" cellspacing="0" width="40%" align="center" style="margin-top:60px">
<tr>
<td>User Name</td>
<td >
<input type="text" name="uname"/>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="pass"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
<input type="reset" value="Clear ">
</td>
</tr>
</table>
</body>
</html>
这个问题是即使用户名密码正确,它也会重定向到 index.php。在这种情况下,我无法弄清楚我缺少什么。代码似乎是正确的。