doqpm82240 2013-03-19 13:35
浏览 6
已采纳

注销后必须刷新页面才能获得正确的代码

$loggedin = false; 
if ($_SESSION) { //user loggedin
    $loggedin = true;
    ...//get token
}
...
if($loggedin){
echo 'Hi '.$user['name'];
}
else{
echo 'Please log in';
}
...

I suppose the web page will display "please log in" when I log out. But it says "undefined $user variable at /src/myproject/index line 80". And after I refresh the page, it says "please log in".

What is the problem here? Thank you for your help.

  • 写回答

1条回答 默认 最新

  • doulan9287 2013-03-19 14:22
    关注

    From what I can tell from your pseudo code, you have some sort of key in the $_SESSION variable that says the user is logged in.

    For demonstration, let's assume you do something like... After the user logs in, you assign $_SESSION['user'] = an array of user information. One of those keys is 'name'.

    So, your code should look something like this

    $loggedin = false;
    if (isset($_SESSION['user'])) {
        $loggedin = true;
    }
    
    if ($loggedin) {
        echo "Hi " . $_SESSION['user']['name'];
    }
    else {
        echo "You are not logged in."
    }
    

    Please keep in mind this is just a solution for your code sample you posted. To do this properly, I would suggest the following changes:

    • create a class that handles authentication
    • create methods in that class to determine if the user is logged in or not
    • create methods to return the current logged in user.

    This will make your code more extensible, reuseable and easier to follow in the future.

    Best of luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据