How do check if a PHP session is empty?

后端 未结 8 1126
無奈伤痛
無奈伤痛 2020-12-02 15:50

Is this bad practice?

if ($_SESSION[\'something\'] == \'\')
{
    echo \'the session is empty\';
}

Is there a way to check if its empty or

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 15:54

    I know this is old, but I ran into an issue where I was running a function after checking if there was a session. It would throw an error everytime I tried loading the page after logging out, still worked just logged an error page. Make sure you use exit(); if you are running into the same problem.

         function sessionexists(){
            if(!empty($_SESSION)){
         return true;
             }else{
         return false;
             }
          }
    
    
            if (!sessionexists()){
               redirect("https://www.yoursite.com/");
               exit();
               }else{call_user_func('check_the_page');
             } 
    

提交回复
热议问题