473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Possible problem with session_start() not resuming session

The essence of my problem is this.

1. I login to my website with user/password. [on index2.php]
2. I select any option, which effectively re-calls index2.php with some additional parameters, to control whats displayed next. Immediately at this point, it logs out the user.

It is logging out the user because after checking $_SESSION['LoginStatus'] == '1' - it finds that its not 1, and therefore displays login section again and a related message.

The website is being run on a shared host environment, which I thought might be a factor, as some people think that session data could be lost due to PHP being compiled with FastCGI instead of ISAPI.

Here is a segment of code at the start of index2.php which sets the session data initially:


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ini_set("session.gc_maxlifetime", 3000);
  3. ini_set("session.use_cookies", 1);
  4. ini_set("session.use_trans_sid", 0);
  5. session_start();
  6.  
  7. include("include/reg_variables.php");
  8. include("dbconnect.php");
  9.  
  10. include("arrays/propdetails.php");
  11. include("arrays/countries.php");
  12. include("arrays/regions.php");
  13. include("arrays/services.php");
  14. include("include/functions.php");
  15. include("web_config.php");
  16.  
  17. if ($_SESSION['LoginStatus'] != '1')
  18. {
  19.     if ($act == 'login') include("include/login.php");
  20. }
  21.  
  22.  
  23. $ow = $_SESSION['UserId'];
  24. $id = $_REQUEST['id'];
  25.  
And here is the login.php:


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if($act=="login")
  3. {
  4.     $dbq = "SELECT * FROM users WHERE username='$username' AND active='1' LIMIT 1";
  5.     $res = mysql_query($dbq) or die(mysql_error());
  6.     $obj = mysql_fetch_assoc($res);
  7.     $mdpass = $obj['password'];
  8.  
  9.     if ($mdpass==md5($password))
  10.     {
  11.         //setcookie("UserId", $obj['id'], time()+3600);
  12.         //$_COOKIE['userid'] = $obj['id'];
  13.  
  14.         $_SESSION['logged'] = 1; 
  15.         $_SESSION['ow'] = $obj['id'];
  16.         $_SESSION['firstname'] = $obj['firstname'];
  17.         $_SESSION['familyname'] = $obj['familyname'];
  18.         $_SESSION['email'] = $obj['email'];
  19.  
  20.         $_SESSION['LoginStatus'] = 1; 
  21.         $_SESSION['UserId'] = $obj['id'];
  22.         $_SESSION['UserFirstName'] = $obj['firstname'];
  23.         $_SESSION['UserFamilyName'] = $obj['familyname'];
  24.         $_SESSION['UserEmail'] = $obj['email'];
  25.     }
  26. }
  27.  
  28.  
  29. if($act=="logout")
  30. {
  31.     //setcookie ("UserId", "", time() - 3600);
  32.     //session_unregister('logged');
  33.     //$_SESSION['logged'] = '';
  34.     $_SESSION['LoginStatus'] = '';
  35.     session_destroy();
  36.     session_unset();
  37.     header("Location: ".$domain."index2.php");
  38.     exit();
  39. }
  40. ?>
  41.  
The problem occurs in both IE6 and FF2 consistently.

I have already changed the session save_path to a different directory on my webspace, and just running out of ideas.

I see that the server directory storing sessions, has 2 session files in it, even though I have only started ONE session. [And I am sure this isnt from a previous session as I deleted all the session files manually on the server as part of the test].

I have reason to believe that because index2.php is re-called with different arguments in the URL, that its calling session_start() again [as it should], and session_start is failing to resume the session and is instead creating a new session. Its therefore lost the details about the user being logged in.

Can anyone suggest any reasons why calling session_start() again would NOT resume the session?
May 22 '07 #1
2 4306
I seem to have found the solution, as I tested 3 times and it was working:

I added the following code in my index2.php file:

ini_set('session.cookie_domain','.yourdomain.com') ;

Of course, I could also have edited the php.ini file to set the cookie_domain too.
Its possible that some browsers have a problem when the cookie_domain is just left empty.
Setting in php.ini would be the better 'full' solution I think.
May 22 '07 #2
Atli
5,058 Expert 4TB
This can happen if the SessionID that is generated for you is lost as you refresh / redirect.

Try printing the session_id(), it should stay the same as long as you do not destroy the session. If it changes your browser is not able to keep track of it so your PHP is constantly creating new Sessions.
If this is the case I would assume that your PHP.ini is configured incorrectly, you might have to enable cookie support or set some temp file path. I know that on Windows some temp paths must be set.

I would also reccomend against using the session_destroy() function but rather unset each variable using the unset() function. It is far safer and less likely to cause "colleteral damage".
May 22 '07 #3

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

Similar topics

0
by: Skip Montanaro | last post by:
Connections to remote Oracle databases doesn't seem possible with DCOracle2. I have no problem making remote connections using Perl's DBI::Oracle package, so I know it's possible in theory. Is...
5
by: Aaron | last post by:
Normally if you want to retreive some data say a html page you would have to send a request to the server. For example a stock ticker sends a request and the server sends back some data every...
6
by: Ruben Charles | last post by:
Hi, I am reading some essays --http://www.paulgraham.com/avg.html "Beating the averages"-- and i am very interested in the web-based applications. I want to take the advantages having the...
1
by: Andreas Klemt | last post by:
Hello, how can I kill all running sessions when working with ASP.NET Session State Service? Is there any VB.NET code for that? Thanks for any help in advance! Andreas
4
by: Lord0 | last post by:
Hi there, Is the following possible with XSLT? Given the following example XML docs: <!-- doc 1--> <user> <username>myUsername</username> <password></password> <phone>12345</phone>
8
by: jaibux | last post by:
Everybody knows how to open (or clone) the same page that you are viewing in a new browser window by CTRL+N or via File->New Window. the question is how to PREVENT to open the SAME WEB APPLICATION...
4
by: Lord0 | last post by:
Is it possible to validate/check for the following using a schema? <question answer="yes"> <answer>This is the answer</answer> </question> OR <question answer="no" />
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.