Forum Moderators: coopster
page1.php
-------------
session_start();
$_SESSION['one'] = 'one';
header("Location: $server/page2.php");
exit();
-------------
page2.php
-------------
session_start();
if ($_SESSION['one'] == 'one') { die("It worked."); }
else { die("It did not work."); }
-------------
(page2.php outputs "It did not work.")
I tried using session_write_close() which was supposed to fix the problem, but alas...
Now I can't be the first to have this error - in fact I've seen plenty of other people with it. What I haven't seen is a solution that will fix this problem. If you have an url to the problem please post it.
Please advise.
something you might want to try is to check out what is in the session.
try this on page2.php
session_start();
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
if ($_SESSION['one'] == 'one') { die("It worked."); }
else { die("It did not work."); }
that will output the full session and all it's values before it makes the comparison. That may help you understand what is going on.
I've been doing php for a year, I am familiar with quite a lot of it...
It's almost an insult reading stuff like "the header function will throw errors"...
Really, I guess I am to blame, I should have set the level to begin with.
To answer the question (hehe...) it is set before I redirect.
My guess is that you've got cookies off when you try this. I copied your code and tried it on my machine (Debian 4.3.4 SAPI) and it worked, both with absolute and relative urls - btw, I really like relative urls a lot better than absolute, they make my code a lot more portable - if you code properly, things work well with a tiny bit more effort. It didn't work with cookies off, but PHP's session machine isn't designed to work with header() - it only modifies the HTML in the buffer, links and such.
So turning my cookies off, I got this code to work in page1.php, page2.php being the same:
<?php
session_start();
$_SESSION['one'] = 'one';
header("Location: /tmp/page2.php?PHPSESSID=".session_id());
exit();
At any rate, happy coding, and I hope you find something to end these location / session woes!
Your issue certainly seems to be more configuration related than anything else. Are you using cookie-based session management or SID?