[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

php session lost on each page

Posted on 2011-09-02
12
Medium Priority
?
421 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 1000 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 84

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 84

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 111

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 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

649 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