Solved

php session lost on each page

Posted on 2011-09-02
12
418 Views
Last Modified: 2012-05-12
For some reason I am losing my php session variables on each page.
I am testing with a simple script.  First page assigns session and I can echo the session variable.
When click on link to another page session is lost and new session id is assigned.

Can someone explain to me why this is happening.

0
Comment
Question by:vzorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 250 total points
ID: 36476413
Is the second page in the same domain as the first?   Session variables do not persist across domains.
0
 

Author Comment

by:vzorn
ID: 36476418
It is the same, however I am in development on a server from Hostgator. The url is like this
http://174.122.60.220:8880/sitepreview/http/tattoo-ins.com/page1.php

I do not switch URL's though.  
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36476435
ALL pages in a session must start with session_start() http://www.php.net/manual/en/function.session-start.php  That initializes and identifies the appropriate session and enables you to access the variables associated with that session.
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:vzorn
ID: 36476447
Yes, have it...  see code attached

Page1.php

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');

echo "Welcome to page #1";
$_SESSION['favcolor'] = "green";
$_SESSION['animal'] = "cat";


echo "<br /><a href='page2.php'>page 2</a>";

$phpSessionId = session_id();
echo $phpSessionId;

echo $_SESSION['favcolor'];
?>

Page2.php
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');

$phpSessionId = session_id();
echo $phpSessionId;

echo "Welcome to page 2<br />";
echo $_SESSION['favcolor']; // green

?>

Open in new window

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 36476471
Put the two scripts into the same directory.  If the session variable is still lost, then the problem is in the code.  If not, then the problem is in the domains/urls.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36476475
The code works perfectly if you make them properly formatted HTML pages.
Page1.php
Page2.php
0
 

Author Comment

by:vzorn
ID: 36476485
These files are in the same directory.   I am testing with this code because it is so basic.  This is all that is in the files.    The other pages I have are more complex but they are also losing session variables.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36476651
Please install this little script and try using it.  If the cheese variable increments your session handler is working correctly and the problem is in your programming.  If not, the error is something your hosting company must correct for you.  Let's start with that and isolate the responsible party.
<?php // RAY_session_test.php
error_reporting(E_ALL);


// DEMONSTRATE HOW PHP SESSIONS WORK
// MAN PAGE HERE: http://php.net/manual/en/function.session-start.php


// START THE SESSION (DO THIS FIRST, UNCONDITIONALLY, IN EVERY PHP SCRIPT ON EVERY PAGE)
session_start();

// INITIALIZE THE SESSION ARRAY TO SET A DEFAULT VALUE
if (empty($_SESSION["cheese"])) $_SESSION["cheese"] = 1;

// SEE IF THE CORRECT SUBMIT BUTTON WAS CLICKED
if (isset($_POST['fred']))
{
    // ADD ONE TO THE CHEESE
    $_SESSION['cheese']++;
}

// RECOVER THE CURRENT VALUE FROM THE SESSION ARRAY
$cheese = $_SESSION['cheese'];


// END OF PROCESSING SCRIPT - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<html>
<head>
<title>Session Test</title>
</head>
<body>
Currently, SESSION["cheese"] contains: $cheese<br/>
<form method="post">
<input type="submit" value="increment this cheese" name="fred"  />
<input type="submit" value="leave my cheese alone" name="john" />
</form>
</body>
</html>
ENDFORM;

echo $form;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36477611
You might also want to run this script and look to see if Suhosin is installed.  If it is, ask HostGator to move you to a server that does not have it.

<?php phpinfo();
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 36477620
FWIW, I installed the scripts on my server and they work correctly.  I wonder if there could be something about the development environment that is awry.  Maybe you would want to make the tests in this URL: http://www.tattoo-ins.com/ and see if the same thing happens in the "real world."
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 36477703
Your scripts run correctly on my server, too.  It has to be a problem in your server environment.  Contact your webhost support and ask them to check it out.
0
 

Author Comment

by:vzorn
ID: 36490296
It was the "sitepreview" url hostgator provides for each domain.
Once I updated my hosts file with the IP I could view the site with just the url
and the session is staying.  Thanks so much for all your help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question