PHP cookie的应用

 <?php 
     //判断是否提交表单
     if(isset($_POST['dosubmit'])){
         //登陆数据库
      $pdo=new PDO("mysql:host=localhost;dbname=xsphp","root","root");
       
         //查询数据库  PDO的预处理
      $stmt=$pdo->prepare("select id,username,allow_1,allow_2,allow_3 from user where username=? and password=?");
 
        // 执行预处理
      $stmt->execute(array($_POST['username'],md5($_POST['password'])));
         //如果查到了
          if($stmt->rowCount() >0){
             list($id,$username,$allow_1,$allow_2,$allow_3)=$stmt->fetch(PDO::FETCH_NUM);
             //将值赋给变量
 
             $time=time()*24*60*60;
             //设置cookie时间
 
             setcookie("uid",$id,$time,"/");
             setcookie("username",$username,$time,"/");
             setcookie("allow_1",$allow_1,$time,"/");
             setcookie("allow_2",$allow_2,$time,"/");
             setcookie("allow_3",$allow_3,$time,"/");
 
             //设置一个登陆判断
             setcookie('isLogin',1,$time,"/");
 
              header("Location:index.php");
 
          }else{
 
            echo "登陆失败";
 
 
          }
 
     }
 
 ?>
 
 <!DOCTYPE html>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
 
  </style>
 </head>
 <body>
      <h2 align="center">用户登录</h2>
   <form action="login.php" method="post" style="margin-left: 40%;">
    
      username : <input type="text" name="username" value="">
       <p></p>
      password : <input type="password" name="password" value="">
       <p></p>
       <input type="submit" value="登陆" name="dosubmit">
 
   
   </form>
 </body>
 
 
 
 
 
 
 </html>
posted @ 2016-08-18 07:32  6月6日暗  阅读(208)  评论(0编辑  收藏  举报