kalpaitch
kalpaitch

Reputation: 5271

$_SESSION unset issue

I have a bizzare case where a session variable is being unset. It seems to be being caused by the following line:

if($_SERVER['SCRIPT_NAME'] != "/search.php") unset($_SESSION["search"]);

Whereas if I remove the unset() in the if clause it works fine. The curious thing is echo some arbitrary text in place of the unset(), nothing comes out (indicating its all fine).

Can anyone see any possible issues with the above line that might cause the $_SESSION to still be unset, bearing in mind that $_SESSION['search'] is actually an array (and sometimes multidimensional)??

EDIT:

include SERVER_ROOT.'/classes/session.class.php';
$sess = new Session();
session_start();

For example the following will echo out 'hallelujah':

if($_SERVER['SCRIPT_NAME'] != "/search.php") ;
if(isset($_SESSION["search"])) echo 'hallelujah';

But this will not (and the if statement evaluates as false):

if($_SERVER['SCRIPT_NAME'] != "/search.php") unset($_SESSION["search"]);
if(isset($_SESSION["search"])) echo 'hallelujah';

Upvotes: 0

Views: 498

Answers (5)

kalpaitch
kalpaitch

Reputation: 5271

Ok, so my bad, this was actually something to do with the 404 page and htaccess redirects. In effect the 404 page included the same code, and was somehow being included by the htaccess file, which was in turn unsetting the $_SESSION value. Curious, but there wasn't anything wrong with the actual code.

Upvotes: 0

JohnP
JohnP

Reputation: 50019

if($_SERVER['SCRIPT_NAME'] != "/search.php") unset($_SESSION["search"]);
if(isset($_SESSION["search"])) echo 'hallelujah';

IF the first condition is true, it unsets 'search'. Which means the second condition won't fire, which is why you aren't echoing anything. What seems to be the problem here?

EDIT

Added some more debug after a comment

var_dump($_SERVER['SCRIPT_NAME'] != "/search.php")

What's printed when you put this? Is it TRUE or FALSE?

Upvotes: 1

chx
chx

Reputation: 11760

I am confused. What happens and what should happen instead? Here is what your code does: If you are not on search.php then you remove the key search and its value from the array $_SESSION. Care to describe in similar words what you perceive as a problem?

Upvotes: 0

Dech
Dech

Reputation: 1722

Tried commenting out the whole line? I am not sure what purpose it is serving given the context.

Upvotes: 0

alex
alex

Reputation: 490163

unset() doesn't care if the value is an array or multidimensional - it will unset it.

Upvotes: 0

Related Questions