Solved

php session lost on each page

Posted on 2011-09-02
12
380 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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 27

Accepted Solution

by:
yodercm 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 82

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
 

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:yodercm
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 82

Expert Comment

by:Dave Baldwin
ID: 36476475
The code works perfectly if you make them properly formatted HTML pages.
Page1.php
Page2.php
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 108

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 108

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 108

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:yodercm
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now