下面是两个页面 一个是登录页面 登录后跳转到blog.php页面 这个页面包含了下面的cookie页面 为什么跳转到blog.php后显示没有登录呢? 应该是登录的才对啊
<code><?php $pdo=new PDO("mysql:host=localhost;dbname=t1;","root",""); if(isset($_POST['submit'])){ $stmt=$pdo->prepare("select id,username,password from user where username=? and password=?"); $stmt->execute(array($_POST['username'],($_POST['password']))); if($stmt->rowCount()>0){ list($id,$username)=$stmt->fetch(PDO::FETCH_NUM); $time=time()*24*60*60; setCookie("uid",$id,$time,"/"); setCookie("username",$username,$time,"/"); setCookie("isLogin",1,$time,"/"); echo '<script>location="blog.php"</script>'; } else{ echo "登陆失败"; } } ?> <form action="login.php" method="post"> 用户名:<input type="text" name="username"> 密码:<input type="text" name="password"> <button type="submit" name="submit">登录</button> </form> 下面是cookie页面 <?php if(!(isset($_COOKIE['isLogin'])&&$_COOKIE['isLogin']==1)){ echo '您还没有登陆呢!请您登陆'; }else{ echo '您是登陆的'; } ?></code>
下面是两个页面 一个是登录页面 登录后跳转到blog.php页面 这个页面包含了下面的cookie页面 为什么跳转到blog.php后显示没有登录呢? 应该是登录的才对啊
<code><?php $pdo=new PDO("mysql:host=localhost;dbname=t1;","root",""); if(isset($_POST['submit'])){ $stmt=$pdo->prepare("select id,username,password from user where username=? and password=?"); $stmt->execute(array($_POST['username'],($_POST['password']))); if($stmt->rowCount()>0){ list($id,$username)=$stmt->fetch(PDO::FETCH_NUM); $time=time()*24*60*60; setCookie("uid",$id,$time,"/"); setCookie("username",$username,$time,"/"); setCookie("isLogin",1,$time,"/"); echo '<script>location="blog.php"</script>'; } else{ echo "登陆失败"; } } ?> <form action="login.php" method="post"> 用户名:<input type="text" name="username"> 密码:<input type="text" name="password"> <button type="submit" name="submit">登录</button> </form> 下面是cookie页面 <?php if(!(isset($_COOKIE['isLogin'])&&$_COOKIE['isLogin']==1)){ echo '您还没有登陆呢!请您登陆'; }else{ echo '您是登陆的'; } ?></code>
<code class="php">$time=time()*24*60*60 改为 $time=time() +24*60*60</code>
用户关闭了浏览器缓存功能的话cookie就不好时了,建议用session
就是可能你关闭了浏览器的cookie缓存