Skip to main content

$_SESSION created but theres no PHPSESSID in $_SERVER


I'm experiencing some weird problems with SESSION variables on my PHP/Ajax online shopping cart.



When I first view the page, the SESSION is created and works within the page. Then when I navigate to another PHP page within the same directory the SESSION is completely lost. What's weird is that this only happens once . Once the user goes through this process of completely losing their SESSION upon changing page, the SESSION works in full across the entire cart.



I started mailing myself var_exports of both $_SESSION and $_SERVER data on each page view. It seems that when a page is first viewed, the SESSION exists and contains data. However there is no PHPSESSID generated in the $_SERVER['HTTP_COOKIE'] variable. On navigating to another page, the PHPSESSID gets created and the SESSION will start working, but the initial SESSION data of the first page view is lost.



Is there a way to generate a PHPSESSID if one has not yet been generated for the SESSION? Or is this typical behaviour and is irrelevant to my random SESSION loss problem? I'm using PHP 5.2.



Every page in the cart starts the exact same way:




$title="Title";
$keywords="keywords";
$description="description";
@include('../header_cart.php');



And then at the top of header_cart.php there is:




session_start();
if(!isset($_SESSION['active'])){
$_SESSION['active']=$_SERVER['REMOTE_ADDR'];
}


Source: Tips4all

Comments

  1. Have you checked that there is no output before your call to session_start()? (Not even a white-space character!).

    HTTP headers cannot be sent after any output has been flushed so that could be causing the attempt to tell the client the initial session cookie to fail.

    ReplyDelete
  2. Are you switching between http: and https: ? They are sometimes treated as two separate domains, and a key may not be shared between them.

    ReplyDelete
  3. Turns out it was recognizing mydomain.com and www.mydomain.com as separate sessions and was storing 2 cookies with 2 different PHPSESSIDs.

    I added this to my .htaccess file to always redirect mydomain.com/shop to www.mydomain.com/shop for both http and https.

    RewriteEngine On

    #force http://www. to make sure SESSION data is always the same
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{REQUEST_URI} shop
    RewriteRule ^(.*)$ http://www.mydomain.com/shop/$1 [R,L]

    #force https://www. to make sure SESSION data is always the same
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{REQUEST_URI} shop
    RewriteRule ^(.*)$ https://www.mydomain.com/shop/$1 [R,L]

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...