网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/10/15 浏览:3 次 )

本文实例讲述了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代码如下:
复制代码 代码如下:<"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针对cookie操作的队列操作类实例
下一篇:PHP使用header()输出图片缓存实例
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?