0

So I am trying to hide or show options if the user is logged in or not.

I have this simple statement, its always showing no regardless if I am logged in or not.

if( !isset($_SESSION) ){
                    echo "yes";
                }
                else {
                    echo "no";
                }

I have also tried

function __construct()
{
parent::__construct();
$this->is_logged_in();
}


    if(is_logged_in())

        {
            echo "yes";
        }

    else
    {
        echo "no";                      
    }

Neither works, I also think the first one if simpler, but I am not sure what method would be better.

tereško
  • 58,060
  • 25
  • 98
  • 150
Beep
  • 2,737
  • 7
  • 36
  • 85

3 Answers3

2

isset($_SESSION) checks if the variable is set or not and here $_SESSION is already defined.

so in your case !(isset($_SESSION)) is false coz isset($_SESSION) is true and !true is false

To check for the session value try isset($_SESSION['key_you_set']). This will check if the key_you_set exists or not.

Nabin Kunwar
  • 1,965
  • 14
  • 29
1

Assuming you have set an session with name session_id You can retrieve your session information in codeigniter like,

$session_id = $this->session->userdata('session_id');

Now You can check like below

if($session_id==""){
echo "session not set";
}
else{
echo "session set";
}
santosh
  • 799
  • 5
  • 17
-1

I think this helps you ..

$session_id = $this->session->userdata('session_id');

less code but more secure

echo (!empty($session_id ) && isset($session_id) ) ? "session set" : "session not set" ;  // ternary operator 
Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
  • `!empty($var) && isset($var)` is an antipattern that no one should ever use in any project for any reason. https://stackoverflow.com/a/4559976/2943403 – mickmackusa Jul 14 '21 at 04:52