PHP用cookie实现自动登陆

 1  <? php
 2  // 自动登陆
 3  if ( $_COOKIE [ " userName " &&   $_COOKIE [ " password " ] )
 4  {
 5       // 获取登录信息传入数据库进行验证
 6 
 7      //打印登录信息
 8       echo   ' 用户名: ' . $_COOKIE [ " userName " ] . ' <br/>密码: ' . $_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 type="text/javascript">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 type="text/javascript">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 >

 

你可能感兴趣的:(cookie)