v8hadas Posted November 26, 2007 Share Posted November 26, 2007 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... Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 26, 2007 Share Posted November 26, 2007 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 Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 26, 2007 Author Share Posted November 26, 2007 Oh yes I changed to: require_once("_def.php"); session_save_path($SESSION_SAVE_PATH); session_start(); And the problem is still the same.... ??? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 26, 2007 Share Posted November 26, 2007 dont you always start the session before anything else. Quote Link to comment Share on other sites More sharing options...
trq Posted November 26, 2007 Share Posted November 26, 2007 The problem is the data in the $_SESSION array is being lost, or the session_id is changing? Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 26, 2007 Author Share Posted November 26, 2007 The session_id does not change, the $_SESSION array is being lost. Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 26, 2007 Share Posted November 26, 2007 Oh yes I changed to: require_once("_def.php"); session_save_path($SESSION_SAVE_PATH); session_start(); And the problem is still the same.... ??? dont you always start the session before anything else. Nope in this case you set the session save path before you call the session http://uk3.php.net/session_save_path Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted November 26, 2007 Share Posted November 26, 2007 If you are still losing your array you could try setting your php.ini to use cookies only. Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 Mmmm... I set session_start(); as the first line of all of my the php files and session data is still get lost... Has any of you seen similar to this? Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 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... Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 27, 2007 Share Posted November 27, 2007 have you tried to remove session_start(); from the top of menu.php? Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 Yes, I just added now and it seems that it does not matter if it is added or not. Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 27, 2007 Share Posted November 27, 2007 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 Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 27, 2007 Share Posted November 27, 2007 In php.ini (on the server you are having the problem), is session.auto_start set to 0 or 1? PhREEEk Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 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? Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 session.auto_start used to be 0, I changed a few minutes ago to 1 hoping that it would help. It did not really. Quote Link to comment Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 session.auto_start used to be 0, I changed a few minutes ago to 1 hoping that it would help. It did not really. Note that any changes to your php.ini require a server restart. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 27, 2007 Share Posted November 27, 2007 If php is installed as a module, did you restart apache to reload the php.ini, or is it installed as cgi? edit: what Thorpe said PhREEEk Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 27, 2007 Share Posted November 27, 2007 I would start throwing this code in various places to see what the state of the session is at any particular moment... <?php echo "<pre>"; print_r($_SESSION); echo "</pre>"; PhREEEk Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 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. ??? Quote Link to comment Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 Did you read my post above? Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 Thorpe, I think yes. session_save_path(): It is not set anymore, default is used. I have admin privileges on this WinXP box therefore I think it is not a permission problem Apache was restarted after the change was made. What do I miss? Quote Link to comment Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 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? Quote Link to comment Share on other sites More sharing options...
v8hadas Posted November 27, 2007 Author Share Posted November 27, 2007 hmmm... I see " Session lostArray ( ) " Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.