ThemeForest

$_SESSION bug

3356 posts
  • Has been a member for 4-5 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost moderator says

Hey CodeCanyon!

I’ve been running into an issue with PHP that I can’t seem to get my finger on. Perhaps one of you guys knows what’s up.

I have two pages. On both pages, I use session_start() to be able to use the SESSION object. At page 1, I store a variable, and then i use header(location: page2.php) to navigate to the 2nd page. Now here’s the odd thing: sometimes the session at page2 differs from the one at page1 (and is thus empty, or contains different variables with names that I have just edited in the script)

This makes it seem like I’m having multiple sessions running simultaneously, accesing them randomly. It’s highly annoying and unpredictable, and only occurs on the server of my client.

Do you have any suggestions what to do about it? Is it some setting that’s wrong? Should I do something to close the session after writing to it or something, so that the changes are absolutely confirmed and stored? (they sometimes store correctly though)

497 posts
  • Canada
  • Sold between 1 000 and 5 000 dollars
  • Has been a member for 4-5 years
  • Exclusive Author
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
TutelageSystems says

Do you know what the lifetime of the session is set to? (phpinfo).

As soon as you write something to the session it is there. No need to say “save”.

Another try would be to just destroy your session and start over again. I find developing some times I can completely mess up my session so I just get rid of it all and start with a fresh session.

You can also experiment using the session id and passing that into the url (messy but worth a try).

203 posts
  • Bought between 1 and 9 items
  • Canada
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 1 000 and 5 000 dollars
iLochie says

I’ve seen problems of variables where they’re not being set before sending header information. What you could try is making sure the variable has been set before redirecting. Maybe something like,

$_SESSION['hi'] = "test";
while($_SESSION['hi'] != ""){
     header("Location: page2.php");
}

No guarantees that this will work though. Good luck!

3356 posts
  • Has been a member for 4-5 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost moderator says

Thanks guys, I’ll try that! You’ll hear from me :)

4335 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Community Moderator
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 4-5 years
+6 more
Reaper-Media moderator says

I’ve run accross this a couple of times, what I suggest trying is something along these lines:

create a file called session.php which looks like this:

<?php if(!isset($_SESSION['started']){
    session_start();
    $_SESSION['started'] = true;
};

?>

and then in both of the files, right at the top, include(); this file ;)

ou should then find that session works fine :)

g’luck ;)

EDIT : For some reason the forum isn’t parsing my line breaks in the code snippet, but you get the idea :P

3356 posts
  • Has been a member for 4-5 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost moderator says

@Reaper-Media

Thanks! However, the problem I was running into isn’t that the session doesn’t start, but the problem is that it seems to start a new session simultaneously with older ones and then uses them in a seemingly random fashion.

@TutelageSystems

I ended up choosing your solution, as it worked. I called session_destroy() before starting the session on the page where I’d set the variables, and didn’t have to change a thing on the pages where I’d read them out. Worked great for some reason – I didn’t have the problem with simultaneous sessions anymore :) Thanks!

Problem solved! :)

4335 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Community Moderator
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 4-5 years
+6 more
Reaper-Media moderator says
@Reaper-Media Thanks! However, the problem I was running into isn’t that the session doesn’t start, but the problem is that it seems to start a new session simultaneously with older ones and then uses them in a seemingly random fashion.

Haha yeah that solution is for the same problem :D For some reason including the file Whig activated the session across multiple pp files prevents multiple sessions occuring… Sometimes :D

Glad you got it working mate! :)

2333 posts
  • Grew a moustache for the Envato Movember competition
  • Community Moderator
  • Elite Author
  • Contributed a Blog Post
  • Won a Competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Has been a member for 4-5 years
+8 more
dtbaker moderator says

Yea had a similar thing once before.

This worked for me:

http://php.net/manual/en/function.session-write-close.php

But I just had a quick look at the comments and it seems others have header() redirect session issues that were not fixed by calling session_write_close() before the redirect.

3356 posts
  • Has been a member for 4-5 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost moderator says

@Dtbaker I tried session_write_close as well, as someone suggested it to me, but it didn’t seem to nihilate the problem. Luckily, completely destroying the session before writing to it again was perfectly possible within the structure of this project, so I’m graceful for that and will just hope I don’t run into it again ;)

3356 posts
  • Has been a member for 4-5 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Microlancer Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost moderator says

Sorry for the double post..

The problem has returned, and now the Session remains empty at all times! The worst part is, I changed nothing at all. I guess I might have to rely on cookies instead – or does anyone have any other suggestions regarding the cause?

by
by
by
by
by