Forum Moderators: coopster

Message Too Old, No Replies

Session variables and header()

         

OutElf

6:19 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Basically, the following will not work. ($server = server)

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.

OutElf

7:15 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Since there appears to be no solution, and that the error might even be browser-related, I will include instead of redirect.

But it should work...

*mutters

jatar_k

4:27 am on Jan 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hey OutElf,

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.

OutElf

10:03 am on Jan 20, 2005 (gmt 0)

10+ Year Member



yeah... im past that...

but fyi it returns empty
array ()

mincklerstraat

10:24 am on Jan 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you try session_write_close() before calling the header?

Anyways, for most stuff an include is nicer than a redirect I find.

OutElf

12:01 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



"I tried using session_write_close() which was supposed to fix the problem, but alas..."

cient

12:22 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



thats my session, works fine


session_start();
$_SESSION["user_id"] = $user_id;
header("Location: member.php");
exit();

differnces:
- session array identifier, "user_id" and 'user_id', should be no problem, but check it out
- the Location: - url, try relative path, not complete url

OutElf

1:58 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



Relative paths are not recommended...

jatar_k

5:31 pm on Jan 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could also try to use the print_r method before you redirect to the second page, it may not be setting it even before you send it.

the header function will throw errors when you do this and won't redirect but you can then verify that it is being set proprely before you redirect.

OutElf

12:31 am on Jan 21, 2005 (gmt 0)

10+ Year Member



... I appreciate your input and all, but come on... how about some real advice?

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.

mincklerstraat

1:33 am on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey OutElf, it's ok, sometimes we say stuff since we don't know exactly what's likely to be helpful to the person asking. We've all been newbies once, and I still occasionally get newbie-like advice on this board.

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!

jatar_k

7:18 am on Jan 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so how about explaining all the tests you have run so I can not waste any more time then...

and my apologies for trying to help

OutElf

10:03 am on Jan 21, 2005 (gmt 0)

10+ Year Member



mincklerstradt excellent remark.

What actually happens is that the 2nd page has a NEW sessid, which probably means I got two session_start() baked in. Time to purge =)

jatar_k you know I didn't mean it like that

coopster

4:40 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are correct then, OutElf. A more concise post from the beginning would help tremendously. This forum is a very friendly, helpful forum and folks are always willing to donate their time and energy to help others. If you have already run down certain areas, by all means, explain what you have and haven't tried so far and any results that offer insight. Also, even jesting can come across terse at times. If a comment is being made tongue-in-cheek, best to throw down a smiley or something to help set the tone ;)

Your issue certainly seems to be more configuration related than anything else. Are you using cookie-based session management or SID?

OutElf

8:21 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



I am mixing them...