3719 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 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)

499 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in Canada
+2 more
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
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has sold $1,000+ on Envato Market
  • Located in Canada
+2 more
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!

3719 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 more
Joost Moderator says

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

4335 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+10 more
Reaper-Media 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

3719 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 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
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+10 more
Reaper-Media 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! :)

3110 posts
  • Located in Australia
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 5 years
  • Helped several times protecting Envato Market against copyright violations
+16 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.

3719 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 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 ;)

3719 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 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
by