Jump to content

Sessions, self defeating?


monkeytooth

Recommended Posts

I don't know if this is a self defeating concept or not. One of the people I am coding with added this line to the top of the index file

if(!isset($_SESSION)) {session_start();}

 

for some reason that doesn't make sense to me, would that be a self defeating way of starting a session? don't you need to have it just as session_start(); no matter what and not wrapped in a if-else?

Link to comment
https://forums.phpfreaks.com/topic/186271-sessions-self-defeating/
Share on other sites

In some bad designed applications people use this technique to verify wether or not session has already been started. Normally you don't check _SESSION but session_id() instead as when session's hasn't yet been started session_id() will return ""

For example if your application has different sources that all may use sessions then at some point a source may rely on sessions and thus writing something like:

 

$_SESSION['datakey'] = 'datavalue';

 

Now the line

 

if (!isset($_SESSION)) { session_start(); }

 

Evaluates false and session's does not get started the array _SESSION gets filled with data but is not persisted between requests.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.