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
https://forums.phpfreaks.com/topic/139965-solved-weird-session-problem/
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.

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.