Home  >  Article  >  php教程  >  PHP用cookie实现自动登陆

PHP用cookie实现自动登陆

WBOY
WBOYOriginal
2016-06-06 09:54:14786browse
 1 php
 2 //自动登陆
 3 if($_COOKIE["userName"&& $_COOKIE["password"] )
 4 {
 5     //获取登录信息传入数据库进行验证
 6 
 7     //打印登录信息
 8     echo '用户名:'.$_COOKIE["userName"].'
密码:
'.$_COOKIE["password"];
 9 }
10 
11 
12 
13 //提交表单
14 if($_GET['submit'])
15 {
16 //    echo 'login。。';
17     //用户勾选"记住密码"选项,将用户登陆信息写入cookie里
18     if($_GET['remember'== 'on')
19     {
20         setcookie("userName",$_GET["userName"],time()+3600);
21         setcookie("password",$_GET["pass"],time()+3600);
22     }
23     
24     //刷新页面
25     echo '<script>location.href="cookie.php"</script>';
26 }
27 
28 //退出登陆
29 if($_GET['out'])
30 {
31     //设置cookie超时
32     setcookie("userName",$_GET["userName"],time()-3600);
33     setcookie("password",$_GET["pass"],time()- 3600);
34     
35     //刷新页面
36     echo '<script>location.href="cookie.php"</script>';
37 }
38 ?>
39 
40 form action="cookie.php" method="get">
41 table border="1" cellspacing=""  align="center" background="" bordercolor="blue" rules="none">
42 
43 tr bgcolor="cccccc" align="left" >th >th>th >输入信息th>tr>
44 tr>td>用户名td>td>input type="text" name="userName" >td>tr>
45 tr>td>密码td>td>input type="password" name="pass">td>tr>
46 tr>td>input type="checkbox" name="remember">记住密码td>td align="right">input type="submit" value="登陆" name="submit">td>tr>
47 table>
48 a href="cookie.php?out=out">退出登陆a>
49 form>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php中GD库的一些简单使用Next article:PHP 线程安全