Beep
Beep

Reputation: 2823

if statement on session data codeigniter

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.

Upvotes: 0

Views: 1027

Answers (3)

Aman Kumar
Aman Kumar

Reputation: 4547

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 

Upvotes: -1

santosh
santosh

Reputation: 787

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";
}

Upvotes: 1

Nabin Kunwar
Nabin Kunwar

Reputation: 1996

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.

Upvotes: 2

Related Questions