Jump to content

[SOLVED] Weird session problem


chrischen

Recommended Posts

So the user posts a comment and then the $_SESSION['post'] = 1;

Then they are redirected to a new page and this code is executed:

1.	 if($_SESSION['post'] == 1) { //for some reason $_SESSION['post'] is 0
2.	 $notice .= 'Your comment has been posted!';
3.	 echo '<div class="notice green">'.$notice.'</div>';
4.	 $_SESSION['post'] = 0; // as long as this line is here post is always equal to 0
5.	 }

 

However the weird problem is that $_SESSION['post'] is 0 in line one as long as long as line 4 is there. If I remove line 4 then $_SESSION['post'] is 1. However even if I have it equal 123 or another integer on line 4 $_SESSION['post'] still equals 0.

 

Also if I have $_SESSION['post'] equal a string on the first page then it works fine and equals the string on the second page.

 

So basically what's happening is the existence of the code on line four is overriding the value of $_SESSION['post'] to equal 0 as long as it was initially an integer value, even if line 4 is not set to equal 0 (tested line four with a string and it was still overrided to 0).

Link to comment
Share on other sites

Nevermind i just solved it. When the user submits the comments the first time it loads the page again before redirecting and the code to set it to 0 is after the header(location: ) command. I guess php code after that is executed too? Well, I learn something new everyday.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.