php cookies自动登录

时间:2022-03-29 17:24:57
<?php
header('Content-type: text/html; charset=utf-8');
error_reporting(0);
//自动登陆
if($_COOKIE["userName"] && $_COOKIE["password"] )
{
//获取登录信息传入数据库进行验证 //打印登录信息
echo '用户名:'.$_COOKIE["userName"].'<br/>密码:'.$_COOKIE["password"];
} //提交表单
if($_GET['submit'])
{
// echo 'login。。';
//用户勾选"记住密码"选项,将用户登陆信息写入cookie里
if($_GET['remember'] == 'on')
{
setcookie("userName",$_GET["userName"],time()+10);
setcookie("password",$_GET["password"],time()+10);
} //刷新页面
echo '<script type="text/javascript">location.href="cookie.php"</script>';
} //退出登陆
if($_GET['out'])
{
//设置cookie超时
setcookie("userName",$_GET["userName"],time()-3600);
setcookie("password",$_GET["password"],time()- 3600); //刷新页面
echo '<script type="text/javascript">location.href="cookie.php"</script>';
}
?> <form action="cookie.php" method="get">
<table border="1" cellspacing="" align="center" background="" bordercolor="blue" rules="none"> <tr bgcolor="cccccc" align="left" ><th ></th><th >输入信息</th></tr>
<tr><td>用户名</td><td><input type="text" name="userName" ></td></tr>
<tr><td>密码</td><td><input type="password" name="password"></td></tr>
<tr><td><input type="checkbox" name="remember">记住密码</td><td align="right"><input type="submit" value="登陆" name="submit"></td></tr>
</table>
<a href="cookie.php?out=out">退出登陆</a>
</form>