10

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?

itsme
  • 48,972
  • 96
  • 224
  • 345

3 Answers3

14

Check for a valid session id:

$sid = session_id();

For example:

$sid = session_id();
if($sid) {
    echo "Session exists!";
} else {
    session_start();
}
Brendan Bullen
  • 11,607
  • 1
  • 31
  • 40
3

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?

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
3

I think you can simply check by doing something like:

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

}
Smita
  • 4,634
  • 2
  • 25
  • 32