Session variables gets flushed!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cyberhawk
Forum Newbie
Posts: 11
Joined: Tue Mar 02, 2004 7:49 pm

Session variables gets flushed!!

Post by cyberhawk »

Hi folks

Im having trouble with sessions, or, no.. i dont.. its the way i try to load the second page, it seems that the javascript i use to load the second page destroys the session or atleast its variables. Its probably acts as ive open a complete new window.. and ofcourse theres no session then.. now how do i solve this?

I cant use "header", because of how my "real" project is coded, and i dont wanna use cookies... i want a session friendly solution. :)

Ive made 2 test pages to help illustrate what my problem is.


first page:

Code: Select all

<?php
session_start();

$un = "anders";

$_SESSION['uname']=$un;
$_SESSION['ualias']='olle';


	echo("<script language=JavaScript>"); 
	echo("self.location.replace('http://cyberhawk/cyberhawk/testx2.php')"); 
	echo("</script>");
?>
Second page

Code: Select all

<?php
session_start();

echo '<pre>'; 
print_r($_SESSION); 
echo '</pre>';

echo $_SESSION['uname'];
echo $_SESSION['ualias'];
?>

Hope you wizzards can help me out.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

if used javascript re-directs before, and it hasnt affected my sessions. also ive even used pop-up wondows, as best i can tell, if the page is called from the browser window with the sessions, the sessions get sent to that window also.

have you checked to see if any session files are being created on the server?
User avatar
emperor
Forum Newbie
Posts: 16
Joined: Tue Mar 02, 2004 11:20 am
Location: UK

Post by emperor »

Just to confirm really - I copied & pasted your code directly and it worked absolutely fine on my comp. If you changed the session files' directory in php.ini, make sure that PHP has write permission. Other than that I'm not sure what else it could be.
cyberhawk
Forum Newbie
Posts: 11
Joined: Tue Mar 02, 2004 7:49 pm

Its strange, this works just fine

Post by cyberhawk »

Hrm.. its really strange... this code works fine.. but not the other code.


First page

Code: Select all

<?php
session_start();

$un = "anders";

$_SESSION['uname']=$un;
$_SESSION['ualias']='olle';
echo '<pre>'; 
print_r($_SESSION); 
echo '</pre>'; 
?>

<p><a href="test4.php">Page 2</a></p>
Second page

Code: Select all

<?php
session_start();

echo '<pre>'; 
print_r($_SESSION); 
echo '</pre>';

echo $_SESSION['uname'];
echo $_SESSION['ualias'];

?>
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Session variables gets flushed!!

Post by Roja »

cyberhawk wrote:echo("self.location.replace('http://cyberhawk/cyberhawk/testx2.php')");
The per-session cookie is being set to the FULL qualified domain name - which is most definitely not the HOSTname "cyberhawk".

Find out what the domain is on the cookie that is set.. in Mozilla its trivially easy - I don't know about how to do so in other browsers.

If the session cookie doesnt get set for the correct domain, its going to issue a NEW cookie - without that info - giving the results you are seeing.

Once you find the correct FQDN, change the cyberhawk to whatever it is (possibly cyberhawk.localdomain), and it should work fine.
cyberhawk
Forum Newbie
Posts: 11
Joined: Tue Mar 02, 2004 7:49 pm

Post by cyberhawk »

hrm.. naa the 2 files lay in the same dir.. cyberhawk... on my localserver... cyberhawk.. i know, confusing names.. :)

well, i've tryed this:

Code: Select all

echo("self.location.replace('testx2.php')");
and this:

Code: Select all

echo("self.location.replace('testx2.php?PHPSESSID=".SID."')");
without any progress.. :cry: it drives me nuts..
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

cyberhawk wrote:hrm.. naa the 2 files lay in the same dir.. cyberhawk... on my localserver... cyberhawk.. i know, confusing names.. :)
Perhaps you don't understand.

the url - http://cyberhawk - means "Go to the DOMAIN cyberhawk". (actually, technically, its much more complicated than that, but for simplicities sake - its looking for the domain cyberhawk.).

Much like http://cyberhawk.com is different from http://cyberhawk.net , so too is http://cyberhawk.

Again I say - Look at the session cookie, and see what the domain is that it is set to. Its *NOT* cyberhawk. Thats not a FQDN - its simply a hostname. Much like "localhost" is a host name and the FQDN is actually "localhost.localdomain" or "127.0.0.1".

From all of your posts, I am fairly certain it is a domain issue - the cookie is being issued for two different domains. The first cookie at "cyberhawk.", and the second likely at the ip address, or the FQDN (perhaps "cyberhawk.localdomain").

If you look at the cookie, you should see what the FQDN is that the cookie is being placed at, and then you can figure out where you SHOULD be redirecting to.
cyberhawk
Forum Newbie
Posts: 11
Joined: Tue Mar 02, 2004 7:49 pm

Post by cyberhawk »

ITS BEEN SOLVED!!!

The following code fixed my problem:

Code: Select all

<?php
echo("<script language=JavaScript>"); 
echo("self.location.replace('http://cyberhawk/cyberhawk/_com/default.php?".SID."')"); 
echo("</script>"); 
?>
But Im still abit puzzled about it. 8O

Thanks for your input 'Roja', maybe you could help me out with this:

Why is it that when i run the code where you have to manually click yourself to the next page, the session id is handled automaticly, BUT when i redirect with the javascript i HAVE to send it by hand in the script?
kingkong
Forum Newbie
Posts: 5
Joined: Tue Mar 02, 2004 7:09 pm

Post by kingkong »

Perhaps its the nature of the javascript location.replace() function, it removes the current histry entry before loading next URL. Maybe it also destroys bits of the session associate with it. That's why you had to pass session ID :?:
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

cyberhawk wrote: Why is it that when i run the code where you have to manually click yourself to the next page, the session id is handled automaticly, BUT when i redirect with the javascript i HAVE to send it by hand in the script?
Because PHP's auto-append session id on urls only generally does so for A hrefs - doing so in javascript can cause problems.

If you change your php.ini to not append session id's on the url, you'd have consistent behavior between them. Point being - you shouldnt assume that is always on. :)
Post Reply