JavaScript和PHP会话

时间:2014-11-10 11:18:16

标签: javascript php session authentication

我的login.php看起来像:



if ($count == 1){
$_SESSION['username'] = "1";
    if (isset($_SESSION['username'])){
        ob_start();

        $url = 'main.php';

        while (ob_get_status()) 
        {
            ob_end_clean();
        }
        header( "Location: $url" );
    }
}
else {
    $_SESSION['username']='';
}




当我成功登录时,main.php会将我重定向到login.php而不是运行游戏循环。



<?php
if(!(isset($_SESSION['username']) && $_SESSION['username']!='')) {
    echo "<script>window.location.href=\"login.php\"</script>";
}
else {
echo "<script defer>reload();</script>";
}
?>
&#13;
&#13;
&#13;

谢谢。

2 个答案:

答案 0 :(得分:2)

尝试改为

<?php
session_start();

if(empty($_SESSION['username'])) {
 echo "<script>window.location.href=\"login.php\"</script>";
}
else {
echo "<script defer>reload();</script>";
}

Empty的检查

"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value)

$_SESSION['username']==''检查。

答案 1 :(得分:1)

我认为你的主要PHP代码应该像这样改变..

<?php
if(!(isset($_SESSION['username']) && $_SESSION['username']=='')) {
    echo "<script>window.location.href=\"login.php\"</script>";
}
else {
echo "<script defer>reload();</script>";
}
?>

您正在检查$_SESSION['username']!=''

需要将其更改为..

if(!(isset($_SESSION['username']) && $_SESSION['username']==''))