tags:

views:

63

answers:

3

I have a if/elseif statement that I am passing a GET variable into. Once inside the corrent block, I can echo something to make sure that I am in the right place. Once there, I have tried several times to set a session variable but it isn't working properly. I'm sure that I'm not doing something right.

if(isset($_GET['mygetvar'])){
  $_SESSION['flag'] = 'on';
}else{
  $_SESSION['flag'] = 'off';
}

When I check the link to take me to the first block, I'm expecting to see the flag on but I get nothing at all. I have already started the session at the very top of the page. All I want to do is use this session as a flag. If its on, do something else don't. I basically want to use this as a switch.


EDIT: Here is the actual code. The avove was an example. If you guys should find a syntax error in this code, then there really is an error. I dont think so because I can echo what's inside the code blocks just fine.

if(isset($_GET['out']))
{
    $_SESSION['rim'] = 'on';
    echo 'out';
}
else
{
    $_SESSION['rim'] = 'off';
    echo 'in';
}


Ok, here is what is going on.... When I do print_r($_SESSION); inside the if/else blocks, I can see the correct results echo. Is this normal?

+2  A: 

Did you remember to call session_start() beforehand?

Amber
Yes, I think I mentioned it already in my post
Try dumping out the contents of $_SESSION using print_r() - see what's set, if anything.
Amber
Hi Dav, I did that already, What I get is array(0) { }
The block of code where I am expecting to see "on" gives me array(0) and the block that I expect to see "Off" gives me the right value. I can't seem to get the on block to work.
Would it be possible for you to include the code for the blocks where you're checking the session value, and not just the one where you're setting it?
Amber
@Jim: that sounds like there's a typo in the name "`_SESSION`" in the actual code. Also, there was no mention of `session_start()` until Dav brought it up.
outis
A: 

$_SESSION is an array. You should have

if($_GET['mygetvar']){

Note the square bracket around $mygetvar

EDIT: And you should probably do

if(isset($_GET['mygetvar'])){
David Archer
Thanks David. That is another typo. Its late and I'm really exhausted and getting quite disgusted with this thing. :) In my actual code, I do have the brackets around the get var. Actually, as I look at yours a bit closer, it seems that you also have a typo. I think it should be if(isset($_GET['mygetvar']))
hahaha yeah true, I'll edit.
David Archer
its possible that your error is elsewhere, try echo $_SESSION['rim'] after setting it inside the block. If that shows the correct value, then your problem is not with setting a session var
David Archer
Hey Dave, I think I may have found something interesting. Please see what you make of it. I edited my original post.
That means (looks like) you're setting the session vars just fine. So now the question is, what are you trying to do with that value and why is it not working. Maybe start another question, referencing this one??
David Archer
+1  A: 
if($_GET(['mygetvar']))

is wrong ($_GET is not a function, but an array). Also, you probably want to be testing whether that query param exists or not, so try

if(isset($_GET['mygetvar']))
Paul Dixon
Thanks Paul, I simply forgot to include the the isset *function*.