Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Session variable not being set.

I send a link in an email to clients so they can verify their booking.

The link is for instance, http://mydomain.com/confirm.php?id=nnnnnn 

where id is the booking number.

In confirm.php I check if the client is logged in

<? if (!isset($_SESSION['clientname']))

     {   $_SESSION['referrer'] = 'http://mydomain.com/confirm.php?id='.$_GET['id'] ; // not logged in - store referring page and id
         
          header("location: http://www.mydomain.com/login.php");  // and send them to login page.

     }

?>

In login.php

after successful login ..

<?
   if (isset($_SESSION['referrer']))                    
      header('location: '.$_SESSION['referrer']);   // return to referrer

   else .....

The problem is $_SESSION['referrer'] is not getting set, although its going thru the block of code and redirecting to login.php.

If I manually type in the URL , the variable is set and all works fine.

Any ideas please?
0
chrisfixit
Asked:
chrisfixit
1 Solution
 
ClickCentricCommented:
You are calling session_start(), right?  If so, then before the header() call to redirect to login.php, try calling session_write_close();
0
 
chrisfixitAuthor Commented:
No luck,

If I take out the redirect and just use echo to check $_SESSION['referer'] the URL is there!

But if I put echo $_SESSION['referer'] as the first statement in login.php (after session_start!!) it's empty.

Somehow it's losing the data.



0
 
ClickCentricCommented:
session_start() is at or near the top of the files, right?  And you're not going to any pages where session_start() isn't called in between them, are you?  login.php has session_start() in it as well?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chrisfixitAuthor Commented:
Top lines in confirm.php (just changed domain name!)

<?php
session_start();
?>
<? if (!isset($_SESSION['username']))
       {$_SESSION['referer']='http://www.mydomain.com/confirm.php?id='.$_GET['id'];
       session_write_close();
       header("location: http://www.mydomain.com/login.php");}
?>

First lines in login...

<?php
session_start();
?>
<?
      echo $_SESSION['referer'];
.....

Nothing there!!!
0
 
ClickCentricCommented:
Hmm...that's strange.  Try checking for the referer session variable before the redirect to the login, but after the session_write_close call.  I'm not sure, but you may be getting a race condition.
0
 
chrisfixitAuthor Commented:
Ok , even more bizarre, I checked as you said and referer was set in both cases. I created a new login.php with just
<? session_start() ;
echo 'hello';
echo $_SESSION['referer'];
echo 'goodbye' ;
?>

At first it just produced hellogoodbye but after a few attempts, just re-clicking the link ' referer' suddenly appeared!!

I rebooted and tried again and it was back to hellogoodbye
0
 
ClickCentricCommented:
Hmm...this sounds like either a race condition, or a storage condition.  Do you know where your sessions are being stored?  Create a .php file with just

<?php
phpinfo();
?>

In it, upload it and point your browser to it.  Look for a setting for session.save_path.  I'm hesitant to ask you to post it here as it can have revealing information in it, but it's one of the things that really needs to be checked.  At the least, check to make sure that the webserver user has read and write access to it.  It's sometimes stored in shared memory which is fast, but flaky.  It could be a permissions issue, though.  Check your access and error logs.  Something may be showing up in there.  And you should probably check with your hosting provider.  Particularly if they have a knowledgebase.  Something like that probably would not be unique.  Oh, and the big one, some hosting providers make the big mistake of storing all sessions in a shared /tmp directory.  The problem is, the sessions can overlap.  Try delaying the forwarding from the linked page to the login page.  Output an html file with a refresh meta tag that points to the login file that just contains a link to the login file so that if the page doesn't refresh they can click on the link.  I have a feeling that if you burn some time between the calls to it, it may start working.
0
 
chrisfixitAuthor Commented:
I've found out that the session variables are stored in a temp folder. But I can make a custom php.ini file and use my own. I'll give that a blast and let you know. Thanks a lot.
0
 
aminerdCommented:
Cookies turned on? :-)
0
 
keteracelCommented:
If you're using IE you need extra headers...

QUOTE:

Microsoft Internet Explorer Sessions Fix

There is one other little tricky issue I must tell you about. Since we all know even though Microsoft makes every attempt to dominate the world, their software still has issues. Let's take IE 6 for example. There's a problem with IE6 that when you are using sessions and you post to a form, when you click your back button to make changes in the form, you have to click the REFRESH button on that page to get the information that you posted back into the form. This only works about 50% of the time, the other 50% the users information is lost and they have to type it over again. Not a good thing if you are trying to get the person to enter their billing information to process an order. They might just get irritated and leave. So, here's a solution for that. Enter this right below the session_start() of each script (yes it still must be before anything is output to the browser).
PHP Example: (!)
header("Cache-control: private");

END QUOTE, from: http://www.phpfreaks.com/print.php?cmd=tutorial&tut_id=41

I tore my hair out with a similar problem. Turns out IE is very 'special' when it comes to it's headers and session variables!
0
 
chrisfixitAuthor Commented:
Thanx, but  already had that bit - took me ages to find it too! Anyway, it turns out that my hosting provider is migrating all its sites to new servers and I think my problem is related to this. They say there are a few issues(!) that they are sortng out!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now