Anyway to tell if a session has started in PHP?

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
test to see whether one of your $_SESSION variables exists, or do something like:

if(!session_name()) //i THINK this will work


link to documentation.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: Zugzwang152
test to see whether one of your $_SESSION variables exists, or do something like:

if(!session_name()) //i THINK this will work


link to documentation.

WTF didn't I thinkof that, lol. For some reason I thought I would have to call session_register first, well I would and I did ;).

Thanks :)
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Originally posted by: AFB
Originally posted by: Zugzwang152
test to see whether one of your $_SESSION variables exists, or do something like:

if(!session_name()) //i THINK this will work


link to documentation.

WTF didn't I thinkof that, lol. For some reason I thought I would have to call session_register first, well I would and I did ;).

Thanks :)

If you're using $_SESSION, then session_register() is unecessary, because it is called by PHP automatically when you add a new variable to $_SESSION. For example:

$_SESSION["php"] = "roxoros"; //implicitly calls session_register()

If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().

You can also create a session variable by simply setting the appropriate member of the $_SESSION or $HTTP_SESSION_VARS (PHP < 4.1.0) array.

source
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: Zugzwang152
Originally posted by: AFB
Originally posted by: Zugzwang152
test to see whether one of your $_SESSION variables exists, or do something like:

if(!session_name()) //i THINK this will work


link to documentation.

WTF didn't I thinkof that, lol. For some reason I thought I would have to call session_register first, well I would and I did ;).

Thanks :)

If you're using $_SESSION, then session_register() is unecessary, because it is called by PHP automatically when you add a new variable to $_SESSION. For example:

$_SESSION["php"] = "roxoros"; //implicitly calls session_register()

If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().

You can also create a session variable by simply setting the appropriate member of the $_SESSION or $HTTP_SESSION_VARS (PHP < 4.1.0) array.

source

It's too late, I mean session_start()
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
i think you do have to call session_start() first.

But if you call session_start(), and a $_SESSION variable that is supposed to be there isn't there, that means the appropriate session was not initialized. Example:

PAGE 1
session_start();
$_SESSION["test"] = "wee";

PAGE 2
session_start();
if(!$_SESSION["test"]) {

//if test is not there after starting the session, that means that a new session was
//initialized and hence the session does not exist.

}