Solved

php session lost on each page

Posted on 2011-09-02
12
396 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 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 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 109

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 109

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 109

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 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