Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of Session var lost after calling HEADER() 2

Status
Not open for further replies.

rejoice

MIS
Jun 5, 2002
42
MY
Hi! everybody.

My problem is that the value of the SESSION_VAR in previous page will lost if I call header function. I test it localhost is ok, but cannot work in my web server.

Is it something regarding the settings? Is there any alternatives? Is it safe for me to pass through URL? How to do it?

I will be very appreciate if anyone can help me :)

SOURCE CODE:

1st Page:
=========
<?
session_start();
session_register(&quot;userName&quot;);

$HTTP_SESSION_VARS['userName'] = &quot;Hoon Guan&quot;;
header(&quot;Location: pg2.php&quot;);
?>

2nd Page:
=========
<?
session_start();
?>
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=iso-8859-1&quot;>
</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot; TEXT=&quot;#000000&quot;>
<? echo $HTTP_SESSION_VARS['userName']; ?>
</BODY>
</HTML>
 
Register the variable with session_register on every page.

You may find easier to put all the session things in a common file and include them in each script, cutting down duplicated code and making it easier to manage --BB
 
BB101:
You only have to register a session variable once. After that, invoking session_start() in successive scripts is all that is required to make the session data available. In fact, session_register() isn't required in this context. The manual states (in a Caution box at If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().

rejoice, your code works fine for me in PHP 4.2.3. What version of PHP are you running? Want the best answers? Ask the best questions: TANSTAAFL!
 
This sounds remarkably similar to the way my cookies were dissapearing when I used a Header(location:) call...

The only solution I was able to make work was to use a javascript function to load the next page instead of a header call.

If you want it up and running now, I'd suggest that, otherwise, I believe it's in the setup, try providing PHP and OS version information.

(sleipnir)
What OS are you running 4.2.3 in?

-Rob
 
skiflyer:

Actually, I just got finished upgrading that machine to 4.3.0 (rejoice's code still works). But it's your basic LAMP (Linux + Apache + MySQL + PHP) machine. Testing it with Opera 7.0b2.

You're ran into the header problems running an IIS box, right? I'm wondering if IIS is doing something screwy with the headers -- like maybe caching them and sending only the &quot;Location&quot; header when that header is present, regardless of any other headers. Did you telnet to the server on port 80 and fetch the script by hand to see what headers were actually being sent? Want the best answers? Ask the best questions: TANSTAAFL!
 
I don't want to go hijacking this guys thread... but yes, that's my setup, IIS 4 to be even more problematic.

Apparently I can't telnet to port 80 on the machine (I'm trying now)... guessing it's one of our internal network blocking issues, and I don't have control of it directly. If I can get access what would be the command to fetch the script by hand?

BB, are you running on a *nix box or Windows box? Hoping we're actually having a discussion which can help you here.

-Rob
 
skiflyer:

Accessing an HTML page by telnet is not hard. Assuming that you need to access the script connect on port 80 and issue the following:

GET /mypath/myscript.php HTTP/1.0[enter]
Host: www.mydomain.com[enter]
[enter]

You'll may get a bunch of stuff scrolling by, so you might want to make sure you can either scroll back up the output or log it. Want the best answers? Ask the best questions: TANSTAAFL!
 
Hey guys thanks for your wonderful inputs.

I am using Linux with PHP version 4.0.6 build Oct 30 2001.

Skiflyer, can you please tell me how you do this with javascript?

 
Erm, generally my code resides on the newest PHP (4.3), IIS and NT4/2K, but I do use apache+linux too --BB
 
Is the problem you're having on the IIS box, the apache box or both?

-Rob
 
So this is fun... considering my telnet likes to make me type blind... here's what I get

HTTP/1.1 302 Object Moved
Location: Server: Microsoft-IIS/4.0
Content-Type: text/html
Content-Length: 161

Code:
<head><title>Document Moved</title></head>
    <body><h1>Object Moved</h1>This document may be found <a HREF=&quot;[URL unfurl="true"]http://url/url/cookie2.php&quot;>here</a>[/URL]

Now when I load cookie1.php, it goes ahead and loads cookie2.php automatically... but the cookie isn't set... (this is using your code from yesterday)

-Rob
 
skiflyer:

This is what I get from Apache running my example code:

HTTP/1.1 302 Found
Date: Tue, 07 Jan 2003 23:08:49 GMT
Server: Apache/1.3.26 (Unix) mod_ssl/2.8.7 OpenSSL/0.9.6b PHP/4.3.0
X-Powered-By: PHP/4.3.0
Set-Cookie: foo=bar
Location: /test_code/test_cookie2.php
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

As you can see, Apache is happily sending the set-cookie header. It looks like IIS 4.0 is screwing around with your headers since the &quot;Location&quot; header is also supposed to change the HTTP reply status from 200 to 302. I've found mentions of this in a German web site or two.

Try this variation on the theme. Edit test_cookie1.php to read:

Code:
<?php
setcookie (&quot;foo&quot;, &quot;bar&quot;, strtotime ('2038-01-01 00:00:00');
header (&quot;Refresh: 0; url=/test_code/test_cookie2.php&quot;);
?>

This may fool IIS into allowing the header.

I also notice both on your machine and mine that the expiration attribute on the cookie is not being set. This is because you can't set the expiration to 100 years in the future . The integer you send for the expiration is a Unix Current Epoch time tick, which is 32-bit and will roll over to zero some time in 2038. That's why I changed the first line in the sample script.

When I run the modified script on Apache, it returns:

HTTP/1.1 200 OK
Date: Tue, 07 Jan 2003 23:29:00 GMT
Server: Apache/1.3.26 (Unix) mod_ssl/2.8.7 OpenSSL/0.9.6b PHP/4.3.0
X-Powered-By: PHP/4.3.0
Set-Cookie: foo=bar; expires=Fri, 01-Jan-38 06:00:00 GMT
Refresh: 0; url=/test_code/test_cookie2.php
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

It does the redirect you're looking for in both IE and Opera from Apache.

The HTTP status of 200 may allow IIS to send the cookies you want to set before redirection.
Want the best answers? Ask the best questions: TANSTAAFL!
 
Cant you use a session from page one and then keep all your data in there? Or maybe you can use the javascript
<script>location.href = 'page2.php'</script>

Njoy --BB
 
Sleipnir...

That modified script works, using the refresh header instead of the location header...

Thanks for the help, now for a pure advice question, I'll be soon writing a program which I won't have 100% say over what OS and such PHP is installed on... do you think this method is safe, or should I stick with the javascript?

BB...
Yes, the javascript works dandy (this is definately more an issue of why doesn't the other work as advertised! and as we assumed from the get-go it looks like it's IIS's fault)... the session doesn't work for me because this needs to last persistently regardless of browser closing and reboots and the like.

-Rob
 
I have tried both the <script> and javascript, but the $HTTP_SESSION_VARS still cannot carry forward to another page. What's wrong?

If I use do you think it is safe? How come my webserver does not hide the SID? >:<
 
PHP can pass the session id either in a cookie or by automatically appending the SID to links.

Do you have cookies turned on in your browser?
Are both script pages in the same domain?

Is it possible for you to upgrade your PHP installation? Some of the 4.0.x versions of PHP had problems with session variables. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top