此代码重定向到登录页面,而不是停留在同一主页。

时间:2017-07-29 09:02:42

标签: php session cookies xampp session-cookies

我希望通过会话或cookie访问我的网站。但是在编写此代码之后,页面需要两个条件都是真的而不是一个。 这有什么不对?

<?php
    if(((isset($_SESSION['SUCCESS']) && $_SESSION['SUCCESS'] != "") || 
    (isset($_COOKIE['is_logged_in']) && $_COOKIE['is_logged_in'] != "")) 
     == false){
        $_SESSION['ERROR'] = "Please login first";
        header('location: login.php'); 
    }

?>

2 个答案:

答案 0 :(得分:0)

无需撰写== false。尝试删除它

if((isset($_SESSION['SUCCESS']) && $_SESSION['SUCCESS'] != "") || 
(isset($_COOKIE['is_logged_in']) && $_COOKIE['is_logged_in'] != "")
 ){
    $_SESSION['ERROR'] = "Please login first";
    header('location: login.php'); 
}

答案 1 :(得分:0)

if(isset($_COOKIE['is_logged_in']) && $_COOKIE['is_logged_in'] != ""){
    if(!isset($_SESSION['SUCCESS'])){
        $_SESSION['SUCCESS'] = true;//or whatever you want
    }
}

然后检查会话

if(!isset($_SESSION['SUCCESS']) || $_SESSION['SUCCESS'] == ""){
    $_SESSION['ERROR'] = "Please login first";
    header('location: login.php');
}else{
    //move to home page
}