php利用cookie实现自动登录的方法

2015-01-24信息快讯网

这篇文章主要介绍了php利用cookie实现自动登录的方法,涉及针对cookie的读取判断及登陆操作,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了php利用cookie实现自动登录的方法。分享给大家供大家参考。具体实现方法如下:

html前端页面代码如下:

<html> 
  <head> 
  <title>enter password</title> 
  </head> 
  <body> 
  <form name="forml" method="post" action="cookiebasedpasswordlogin.php"> 
    <table> 
      <tr> 
       <td colspan="2" > 
         <div align="center"><b>please specify the password</b></div> 
       </td> 
     </tr> 
   <tr>> 
     <td> 
       <div align="right">customer id</div> 
     </td> 
     <td> 
       <input type="text" name="username"> 
     </td> 
   </tr> 
   <tr> 
     <td> 
       <div align="right">password</div> 
     </td> 
     <td> 
       <input type="password" name="password"> 
     </td> 
   </tr> 
   <tr> 
     <td colspan="2"> 
       <center> 
         <input type="submit" name="submit" value="login"> 
       </center> 
     </td> 
    </tr> 
   </table> 
  </form> 
  </body> 
</html>

后端PHP代码如下:
<?php 
    $now = getdate(); 
    $storetime= $now["weekday"] . " " . $now["month"] ." " . $now["year"] ; 
    $storetime.=" time : "; 
    if ($now["hours"] < 10) { 
      $storetime.= "0" . $now["hours"]; 
    } else { 
      $storetime.= $now["hours"]; 
    } 
   
    $storetime.= ":"; 
    if ($now["minutes"]<10) { 
      $storetime.= "0" . $now["minutes"]; 
    } else { 
      $storetime.= $now["minutes"]; 
    } 
     
    $storetime.= ": "; 
    if ($now["seconds"] <10) { 
      $storetime.= "0" . $now["seconds"]; 
    } else { 
      $storetime.= $now["seconds"]; 
    } 
    if (isset($data)) { 
       $counter=++$data[l]; 
        setcookie("data[0]",$storetime,time() + (60*60*24)); 
        setcookie("data[l]", $counter,time() + (60*60*24)); setcookie("data[2]",$username,time() + (60*60*24)); 
        echo "<b><center>hi " . $data[2] . " ! !</center></b><br>n"; 
        echo "<b><center>last login time :" .$data[0] . "</center></b><br>n"; 
        echo "<b><center>current date :" .$storetime. "</center></b><br>n"; 
        echo "<b><center>page view count :" . $data[l]. "</center></b><br>n"; 
        echo "<b><center>you have successfully logged in!</center></b>"; 
        echo ("<b><contor>you can access this area without entering a password for the next 24 hours!</center></b>"); 
   } else { 
    if (isset($username) && isset($password)) { 
     if ($password=="superpass") { 
          $counter=0; 
          setcookie("data[0]",$storetime,time() + (60*60*24)); 
          setcookie("data[l]",$counter,time() + (60*60*24)); 
          setcookie("data[2]",$username,time() + (60*60*24)); 
          $url="location: cookieimp.php"; 
          header($url);
     }else{ 
          echo "<hl><center>invalid password!!!</center></hl>"; 
     } 
    } 
} 
?>

希望本文所述对大家的PHP程序设计有所帮助。

php使用正则表达式获取图片url的方法
php使用CURL伪造IP和来源实例详解
php+mysql实现无限分类实例详解
php截取html字符串及自动补全html标签的方法
php在linux下检测mysql同步状态的方法
php通过Chianz.com获取IP地址与地区的方法
php实现utf-8转unicode函数分享
为PHP5.4开启Zend OPCode缓存
腾讯微博提示missing parameter errorcode 102 错误的解决方法
thinkphp中session和cookie无效的解决方法
yii2.0之GridView自定义按钮和链接用法
php第一次无法获取cookie问题处理
CI框架中cookie的操作方法分析
php利用cookies实现购物车的方法
php针对cookie操作的队列操作类实例
PHP使用header()输出图片缓存实例
PHP实现服务器状态监控的方法
PHP里8个鲜为人知的安全函数分析
php实现MySQL数据库备份与还原类实例
两个php日期控制类实例
Codeigniter框架实现获取分页数据和总条数的方法
Yii中CGridView关联表搜索排序方法实例详解
Yii把CGridView文本框换成下拉框的方法
Yii框架获取当前controlle和action对应id的方法
php 删除cookie方法详解
Codeigniter购物车类不能添加中文的解决方法
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
PHP中mysql_field_type()函数用法
php中cookie实现二级域名可访问操作的方法
PHP读取CURL模拟登录时生成Cookie文件的方法
php cookie名使用点号(句号)会被转换
php cookie中点号(句号)自动转为下划线问题
©2014-2024 dbsqp.com