-5

我需要在我的代码中记住我的功能。我设置了会话,但我不知道设置 cookie 以记住我。请在我的登录页面下方设置记住我的代码。我尝试了很多网站,但没有得到准确的输出。

<?php 
session_start();
ob_start();
error_reporting (E_ALL ^ E_NOTICE);
$con=mysql_connect("localhost","root","");
mysql_select_db("pratice") or die (mysql_error());
$username=$_POST['username'];
$password=$_POST['password'];
$remember=$_POST['remember'];

if(isset($_POST['submit']))
{
$sql="select id, username FROM `action2` WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);

if($num>0)
{
    $row=mysql_fetch_array($result);
    $username=$row['username'];
    $_SESSION['id'] = $row['id'];
    $_SESSION['username']=$username;
    header("location:userpage.php");
}
else 
{
echo"<p><center><b><font color='red'>Incorrect Username or Password!</font></b></center></p>";
}
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Log In</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> 
<table align="center">
<tr>
    <td colspan="2">LOGIN</td>
</tr>
<tr>
    <td>Username</td>
    <td><input type="text" name="username" /></td>
</tr>
<tr>
    <td>Password</td>
    <td><input type="password" name="password" /></td>
</tr>
<tr>
    <td align="right"><input name="remember" type="checkbox" value=""/></td>
    <td>Remember me</td>
</tr>
<tr>
    <td colspan="2" align="center"><input type="submit" name="submit" value="LogIn" /></td>
</tr>
</table>
</form>
<br />
</body>
</html>
4

1 回答 1

0

在您当前的文件中,检查$_SESSION['id']and是否$_SESSION['username']已设置。我是的,您可以直接将用户重定向到userpage.php.

if($_SESSION['id'] != "" && $_SESSION['username'] != "")
{
    header("location:userpage.php");
}

如果您想在检查“记住我”时执行此操作,您可以使用 javascript 或 jquery。

于 2012-08-14T05:48:54.923 回答