473,788 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Session Resetting on 2nd Page

LacrosseB0ss
113 New Member
Hey folks;
I have PHP session variables that are having some troubles. I posted in a few other forums but couldn't really find one dedicated to my specific issue. I am using a few session variables throughout my website and am using the following two lines at the top of every session related page:

[php]
<?php
session_start() ;
$_SESSION['userID'] = ""; //initialization on first page used only
?>
[/php]

and then code elsewhere on the page. In every case, the session variable saves fine and works. UNTIL it swaps pages. When the 2nd page is loaded I was using "echo $_SESSION['<name>']" but in every case again, the $_SESSION variable loses the data that was set in code and reverts to whatever it was initialized to (using the above example: if the $_SESSION['userID'] saved "Bob123" on page 1, now it would have " " saved).

I had originally hardcoded values in to move on with development but now I need to get this working and am frustrated with the lack of help search engines and other resources have provided. Thanks in advance for any assistance any of you here in the community can provide.

- LB
Dec 13 '07 #1
8 3304
clai83
41 New Member
Hey folks;
I have PHP session variables that are having some troubles. I posted in a few other forums but couldn't really find one dedicated to my specific issue. I am using a few session variables throughout my website and am using the following two lines at the top of every session related page:

[php]
<?php
session_start() ;
$_SESSION['userID'] = ""; //initialization on first page used only
?>
[/php]

and then code elsewhere on the page. In every case, the session variable saves fine and works. UNTIL it swaps pages. When the 2nd page is loaded I was using "echo $_SESSION['<name>']" but in every case again, the $_SESSION variable loses the data that was set in code and reverts to whatever it was initialized to (using the above example: if the $_SESSION['userID'] saved "Bob123" on page 1, now it would have " " saved).

I had originally hardcoded values in to move on with development but now I need to get this working and am frustrated with the lack of help search engines and other resources have provided. Thanks in advance for any assistance any of you here in the community can provide.

- LB
Please post a little bit more code, for example your second page. If the problem isn't your subsequent pages, then it is probably your php.ini settings.
Dec 14 '07 #2
LacrosseB0ss
113 New Member
I have 3 separate sections where Sessions are used. $_SESSION['userID'] for a user that logs in, $_SESSION['newPwd'] which is used when a user is setting a new password and finally $_SESSION['matchedIDs'] which comes from an XML file based on a search.

In each case the opening 2 lines of the page are:
[php]
<?php session_start() ;
//depending on the page, one of the next 3 lines
$_SESSION['userID'] = "User Not Found";
$_SESSION['newPwd'] = "";
$_SESSION['matchedIDs'] = "No Match Found";
?>
[/php]

Then in the code (last thing before the </head> tag in HTML). (again, depending on the page)
for User ID:
[php]
//snipet $member comes from a master XML file
if ($member->FirstName == $_POST['txtFName'] && $member->LastName == $_POST['txtLName'] && $member->Password == strtoupper($_PO ST['txtPwd']))
{
$_SESSION['userID']=$member->ID;
[/php]

for New Password:
[php]
if (isset($_POST['cmdChange']))
{
$newPwd = $_POST['txtNew'];
$_SESSION['newPwd'] = $newPwd;
}
[/php]

and finally for Matches:
[php]
//again, called from an XML file
function search()
...
//list of matches from user text boxes, matched to XML file
$matchedIDs = $matchedIDs . $member->ID . ", ";
...
return $matchedIDs;

//saved in
$_SESSION['matchedIDs'] = search();
[/php]

Now, all of this saves fine (I've checked with echo statements). However when I go to reference it on the next page, it is back to whatever the original value was (1st code block). This code for these pages is:
[php]
<?php session_start() ; ?> //top of every page

//before the </head> tag - page specific
$member = simplexml_load_ file('/cgi-bin/XML/' . $_SESSION['userID'] . '.xml'); //load XML file for logged in user
$IDs->$pwds->nodeValue = $_SESSION['newPwd']; //change password, update XML
echo $_SESSION['matchedIDs']; //search, temp testing for now
[/php]

Sorry all for the lengthy post and numerous code blocks. But there it is for you. Hope it helps and provides enough info. I'm trying to finish up the project and get it off my plate as soon as I can. Thanks everyone, and thanks to clai83.
- LB
Dec 14 '07 #3
LacrosseB0ss
113 New Member
oh yeah, and I am not the server owner so I don't have access to the php.ini file. Please tell me that's not the issue. Lie if you have to ...
Dec 14 '07 #4
clai83
41 New Member
oh yeah, and I am not the server owner so I don't have access to the php.ini file. Please tell me that's not the issue. Lie if you have to ...
I don't see anything wrong with the code you posted that would affect your $_SESSION['user_id'] variable. I would do the following to check to see if the session is being properly stored.

1. echo the $_SESSION['user_id'] in the first and second page.
2. echo the session_id() to make sure that they are the same.
3. If the session id is not the same then for some reason a new session is being created somewhere.
a) Check the directory where session variables are stored (by default this is /tmp but it could be something else if your server has changed the setting). See if a session file with a matching id is stored in there.
4. Check the phpinfo() and check to see the session settings.
a) session.save_ha ndler = files
b) session.use_coo kies = 1
c) session.use_onl y_cookies = 1
d) session.name = PHPSESSID
pay close attention to the PHP session garbage collector
If these settings put a high probability of deleting your session
data, that may be a reason too, why the session data is disappearing though I doubt this but check it anyway. This is my setting.

session.gc_prob ability = 1
session.gc_divi sor = 1000

5. For testing purposes echo out all your globals to see how data is being propagated. might find something useful there.

6. If all else fails try to manually propagate your session in the URL in a test page and see if the session data keeps.

I'm sorry if I'm being abstract on my possible solutions, I just don't see any thing yet in your code that can cause it to reset to " ".

hopefully this is of some help
Dec 14 '07 #5
LacrosseB0ss
113 New Member
thanks again. Yeah, I couldn't see anything in the code either that would be causing any of these problems. Hence me coming here. Except that I have only been using PHP full time for about 3 months.

Clarification: If I'm using a session variable, do I need session_start() at the top of EVERY page? When I originally heard this, I assumed that would create a new session each time. Is there not a session_continu e? Or is it not implied (as with ASP) until session_destroy ();

I'll investigate your suggestions. Problem being I will need to contact the server owner. As previously mentioned, I don't have access to the server settings (that I know of). The project was thrown together super quick by the sponsors and they got server space for REAL cheap off someone they knew. Ideally, the server would be my baby and I could control what my php did.

Oh well, c'est la vie. thanks again. I'll e-mail our guy and post back.
Dec 15 '07 #6
clai83
41 New Member
thanks again. Yeah, I couldn't see anything in the code either that would be causing any of these problems. Hence me coming here. Except that I have only been using PHP full time for about 3 months.

Clarification: If I'm using a session variable, do I need session_start() at the top of EVERY page? When I originally heard this, I assumed that would create a new session each time. Is there not a session_continu e? Or is it not implied (as with ASP) until session_destroy ();

I'll investigate your suggestions. Problem being I will need to contact the server owner. As previously mentioned, I don't have access to the server settings (that I know of). The project was thrown together super quick by the sponsors and they got server space for REAL cheap off someone they knew. Ideally, the server would be my baby and I could control what my php did.

Oh well, c'est la vie. thanks again. I'll e-mail our guy and post back.
^_^ you do need to session_start() on top of every page. You would think that this would restart a new session, but it doesn't. Basically PHP will do all the background work to see if the user has a cookie (if you are using that method) that has a php session id that matches a session that is currently being stored. If there is a match then all your session variables are stored in the $_SESSION super global

hopefully this is the answer
Dec 15 '07 #7
LacrosseB0ss
113 New Member
it does help explain why session_start() goes on every page. Still haven't had a chance to look at the solutions above yet. But when I do, as mentioned I'll report back. By the sounds of what you're talking about it could be different session IDs which could suck.

I've also been reading about session_registe r(); what does that do? And how does HTTP_SESSION_VA R something like that differ from $_SESSION (which I've seen around). I'm led to believe HTTP whatever it is more of a legacy coding concept replaced by the $_SESSION global.

Again, sorry for basic questions, I'm VERY new at this. Thanks for all the help.
- LB
Dec 16 '07 #8
clai83
41 New Member
it does help explain why session_start() goes on every page. Still haven't had a chance to look at the solutions above yet. But when I do, as mentioned I'll report back. By the sounds of what you're talking about it could be different session IDs which could suck.

I've also been reading about session_registe r(); what does that do? And how does HTTP_SESSION_VA R something like that differ from $_SESSION (which I've seen around). I'm led to believe HTTP whatever it is more of a legacy coding concept replaced by the $_SESSION global.

Again, sorry for basic questions, I'm VERY new at this. Thanks for all the help.
- LB
Yes HTTP_SESSION_VA RS is deprecated. Same thing though.

session_registe r() is a deprecated way of registering new session variables. Just use $_SESSION superglobal for all your session needs.

1. Make sure you allow cookies on your browser when you are testing your script. If the session cookie is not stored properly on your side then the session_start will always bring up a new session.

2. While you are testing your code check the cookie stored on your computer and make sure that the cookie php session id matches the current session you are working with. Just do a quick echo session_id().

3. Even if you are not the server owner, if your server allows htaccess overrides then you can set your own phpini settings using that. Google 'phpini htaccess' and there are a bunch of pages that will show you how.

4. There is also a ini_set function that you can use to set phpini settings through your script. I'm not completely sure about the scope of this function. You may want to consult php.net manual for more details.

In any case do a small test on your server.

For example

PAGE 1
[PHP]
<?php
session_start() ;
$_SESSION["testvar"] = "testing";
echo session_id()."< br />";
?>

<html>
<head><title>Pa ge 1</title></head>
<body>
<p>
<a href="page2.php " title="Page 2">Page 2 Link</a>
</p>
</body>
</html>
[/PHP]

PAGE 2
[PHP]
<?php
session_start() ;
echo $_SESSION["testvar"]."<br />";
echo session_id()."< br />";
?>
[/PHP]

If there are matching session IDs and if your testvar displays right then there is something wrong with your code or the server PHP settings.

Also, remember to put session_start() at the top of your script (before you start displaying things in HTML) otherwise that may also be a reason why your session vars are disappearing.
Dec 16 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5977
by: M Wells | last post by:
Hi All, Just wondering how you go about changing the value of a session cookie via javascript? I have a PHP page that sets a session cookie when it first loads. I'd like to be able to change the value of that session cookie in response to a button click in a form, without resubmitting the page. For some reason, the following doesn't seem to work:
8
18772
by: riprod | last post by:
Someone in the IIS newsgroup suggest I post this here, so sorry in advance for the cross posting. I have a Win 2003 SP1 with IIS 6 and host about 40 websites, most of them useing ASP/VB with access databases. The server is a Dell Dual 2.4Ghz XEON with 512Mb Ram. Every few days I get the error 'HTTP/1.1 New Session Failed' and the server stops showing the sites. It comes right after a bit and then the error comes back. I
5
2676
by: Jon Booth | last post by:
I want to write an apsx page that when loaded does not refresh Session.Timeout. It is going to be running in an iframe and refreshing every couple of minutes. example. User has 15 minutes left until session expires. iframe.aspx loads and session still has 15 minutes left. Is this possible? I was trying to grab session.timeout at the begininning of the page (init)
2
1868
by: Dinkster | last post by:
BackGround: - We are new to ASP - We thought we would experiment with using the session object (in proc) to store a small amount of data. - We get different results when cookieless is set to "true" veses "false". - Our simple test app involved updating a counter that was stored as an item in the Session Object. We allow resetting the counter to zero, and adding or subtracting one from the counter. - We were trying to determine when a "NEW...
3
1764
by: No One | last post by:
This seems to be related to the ViewState error that is now starting to plague ASP.Net apps. When the Application Pool is recycled, the Session seems to be getting reset. When this is done, session information is lost and my app forgets what rules a user has. I'd rather not recode to read this from the database for every page, but I wonder if I have no other choice. Anyone else notice the Session resetting?
1
1164
by: Mike | last post by:
Hey guys need some help on session state. I'm trying to create an ASP.NET page to add items to it. W.G. if your out there I could still use some help! I'm not quite sure how to get it to work. Page loads User selects a value from a drop down list uses a button to add item into a variable
2
2811
by: Doogie | last post by:
Hi, We have a page we want to refresh every 30 minutes so that users can get up to date info. The problem is that there is information within the session that we need in each refresh to determine what roles the user belongs to so that we can get the data they need. The page times out because we lose our session info after 20 minutes. Resetting that timeout value is not an option (I've been told we aren't allowed).
1
7188
by: =?Utf-8?B?TWlrZSBNY0FsbGlzdGVy?= | last post by:
I have not been able to find anything about this problem, so it must be configuration on our server, somehow... What we are observing is on Windows Server 2003/IIS 6 our AJAX UpdatePanels are not resetting the IIS Session timer. Thus, if the default IIS Session Timeout of 20 minutes is used, our logged-in users are always receving Session timeout in 20 minutes, regardless of what UpdatePanel activity is happening on the .aspx page. ...
9
3880
by: MC | last post by:
I would like to display a timer in the corner of the page that shows the user how many minutes:seconds till the session times out. Are there any good examples out there? Google has again failed me ;) MC
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.