Filippo oretti
Filippo oretti

Reputation: 49823

Codeigniter & PHP check if session exist

How can i simply check if cookies are enabled and user session too in PHP?

I need the really lighter piece of code of this world to do that, can anyone show me somenthing?

I'm on Codeigniter but i'm planning to use native PHP for this control.

my code:

if(session_start()){ echo 'started'; }

as i know Codeigniter doesn't uses native PHP session, how to do so?

Upvotes: 10

Views: 24467

Answers (3)

Smita
Smita

Reputation: 4634

I think you can simply check by doing something like:

if(isset($_SESSION)){
  // tells php session is initiated
}
if(isset($_COOKIE)){

}

Upvotes: 3

René Höhle
René Höhle

Reputation: 27305

The first point is "Don't fight the framework" when your framework has some functions than use it. Normally in the Framework classes are functions to prevent injections.

Here is a Post when tells you how to check the cookie:

Check if cookies are enabled

And for the session

How to tell if a session is active?

Upvotes: 3

Brendan Bullen
Brendan Bullen

Reputation: 11808

Check for a valid session id:

$sid = session_id();

For example:

$sid = session_id();
if($sid) {
    echo "Session exists!";
} else {
    session_start();
}

Upvotes: 14

Related Questions