Jump to content

[SOLVED] $_SESSION['Data'] is lost when reloading page


v8hadas

Recommended Posts

Hi There,

 

I am quite new to php (coming from c++) and found some difficulties with the following scripts I could not resolve so far.

The code is structured as:

__dbconnect.php:_sess.php:__def.php

meaning that I load the definitions every single time, then I load the session file every time and I load db connection (pconnect stuff) when it is needed.

 

_sess.php:

<?php
session_start();
session_save_path($SESSION_SAVE_PATH);  // from _def.php
require_once("_def.php");

if (!isset($_SESSION['UserId']) || !isset($_SESSION['UserEmail'])) {
/*print(session_id());
exit(0);*/
$_SESSION['UserId'] = "-1";
$_SESSION['UserEmail'] = "";
$_SESSION['IsTestComplete'] = 0;
}
?>

 

Then I do a couple of steps to authenticate the user (login). Afterwards a customized menu is shown to the user, the above shown session variables are set. As the print/exit line shows, I checked if the session id exists or not and I found that it exists and it looked to be correct for the print. I also checked the stored session file which contained proper information as it was saved into file. However no matter what I do - proper data in file for the session id what is recognized and printed - I got the debug line printed and $_SESSION values reset immediately.

 

Is there someone around who can pop up a good guess what is the cause of this nasty behavior?

 

Thanks...  :)

Link to comment
Share on other sites

don't know if im 100% but shouldn't, don't know if im being stupid but if the variable value for $session_save_path is in _def.php it would need the file to required before you set the session_save_path?? no

 

session_save_path($SESSION_SAVE_PATH);  // from _def.php
require_once("_def.php");

 

be

 


require_once("_def.php");
session_save_path($SESSION_SAVE_PATH);  // from _def.php

Link to comment
Share on other sites

The strange thing is that I have a couple of php files and some are nested. There is one php file which I called "menu.php", it creates the menu on the left. It looks like:

<?php
session_start();
if (strlen($_SESSION['UserEmail']) == 0) {
print("<h1>You have not logged in yet.</h1>");
} else {
print("<h1>You have logged in as " . $_SESSION['UserEmail'] . ".</h1>");
}
?>

<div id="menuspace"> </div>
<div id="content">
<div id="menu">

<?php
if ($_SESSION['Viewing'] == $PAGE_ID_WELCOME) {
print("<a href=\"welcome.php?".session_id()."\" title=\"Welcome\" class=\"selected\"><img src=\"i/m_welcome.png\" class=\"MenuLogo\" alt=\"welcome\" /> Welcome</a>");
} else {
print("<a href=\"welcome.php?".session_id()."\" title=\"Welcome\" class=\"sect\"><img src=\"i/m_welcome.png\" class=\"MenuLogo\" alt=\"welcome\" /> Welcome</a>");
}
.....
?>

 

That file is added to the page showing files by a require() call. The strange thing is that if I remove this require() for that menu.php file then the session data seems to be okay in the file. However, if menu.php runs it resets the session data in the file.

Can it be some kind of recursion problem in the interpreter or do I use php in a way it should not be used?  :-[

Again, this menu.php prints the $_SESSION['UserEmail'] properly but the data is lost after it was included.

 

Thanks...

Link to comment
Share on other sites

try this in menu.php

 

<?
$thesession = session_id();
session_start();
if (strlen($_SESSION['UserEmail']) == 0) {
print("<h1>You have not logged in yet.</h1>");
} else {
print("<h1>You have logged in as " . $_SESSION['UserEmail'] . ".</h1>");
}
?>

<div id="menuspace"> </div>
<div id="content">
<div id="menu">

<?php
if ($_SESSION['Viewing'] == $PAGE_ID_WELCOME) {
print("<a href=\"welcome.php?".$thesession."\" title=\"Welcome\" class=\"selected\"><img src=\"i/m_welcome.png\" class=\"MenuLogo\" alt=\"welcome\" /> Welcome</a>");
} else {
print("<a href=\"welcome.php?".$thesession."\" title=\"Welcome\" class=\"sect\"><img src=\"i/m_welcome.png\" class=\"MenuLogo\" alt=\"welcome\" /> Welcome</a>");
}
.....
?>

 

 

im basing this off of text in docs under the id section

http://www.php.net/manual/en/function.session-id.php

the text seems to contradict itself

Link to comment
Share on other sites

Thanks for the good words. Unfortunately it still has not worked out. I got _sess.php (as per the first post) run and it does not recognize the existing $_SESSION variables in the isset still. Would it be possible that I attach some parts of the php files to see if you find something very obvious which I cannot spot?

Link to comment
Share on other sites

With so many issues, the first thing I would do is remove any calls to session_save_path(). Let php save session cookies where it defaults to via the ini. You may have permissions setup incorrectly on the directory you are trying to set as the save path.

Link to comment
Share on other sites

I did as you told and added the print_r($_SESSION); to the code. I also added print_r(session_id()); to make sure that the session is there.

 

The strange thing is that in the _sess.php (what I posted in the first post) the session id is pinted as correct (I see the actual data in the session file) and still the $_SESSION is shown as an empty Array.  ???

Link to comment
Share on other sites

OK, lets setup a simple test.

 

p1.php

<?php

  session_start();
  $_SESSION['test'] = 'foo';
  echo "<a href='p2.php'>click</a>";

?>

 

<?php

  session_start();
  if (isset($_SESSION['test'])) {
    echo $_SESSION['test'];
  } else {
    echo "Session lost";
    print_r($_SESSION);
  }

?>

 

Put these files in place, browse to p1.php, then click the link. What do you see?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.