Page 1 of 1
Session variables gets flushed!!
Posted: Wed Mar 03, 2004 8:32 am
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.
Posted: Wed Mar 03, 2004 8:45 am
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?
Posted: Wed Mar 03, 2004 10:25 am
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.
Its strange, this works just fine
Posted: Wed Mar 03, 2004 11:55 am
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'];
?>
Re: Session variables gets flushed!!
Posted: Wed Mar 03, 2004 1:45 pm
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.
Posted: Wed Mar 03, 2004 7:38 pm
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..

it drives me nuts..
Posted: Thu Mar 04, 2004 9:39 am
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.
Posted: Thu Mar 04, 2004 4:03 pm
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.
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?
Posted: Thu Mar 04, 2004 7:26 pm
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

Posted: Fri Mar 05, 2004 10:11 am
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.
