Solved

php session lost on each page

Posted on 2011-09-02
12
387 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

948 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

18 Experts available now in Live!

Get 1:1 Help Now