Session data disappears on main page
Moderator: General Moderators
Session data disappears on main page
I have a website that I am implementing PHP sessions to track if a user is logged in. The sessions work fine when I am pointed directly at a page (say, http://www.mysite.com/member.php or http://www.mysite.com/index.php). But if I go to the main site without specifying index.php (i.e. http://www.mysite.com), the session data is intermittent. About 1/3 of the time the session data is there and about 2/3 of the time the session data is missing. Only if I specify the full page, without letting the default page load (in my case its index.php) can I seem to have my session data always show up. Any help would be appreciated.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Session data disappears on main page
How do you determine the current page?
Where are you storing the session information?
How are you storing the data?
I can't seem to find my magic wand, so I'm going to need a lot more information to help you out.
Where are you storing the session information?
How are you storing the data?
I can't seem to find my magic wand, so I'm going to need a lot more information to help you out.
Re: Session data disappears on main page
Ok, here are some more details:
Every page calls session_start(); at the top
I am using the $_SESSION array to store my session variables
So when i navigate to index.php directly, my session info is there and I can access it using something like $_SESSION['logged']
When I navigate to the root webpage which loads index.php by default, the session variables are empty 2/3 of the time.
my php.ini has the following session entries:
[Session]
session.save_handler = files
;session.save_path =
session.use_cookies = 1
; session.use_only_cookies = 0
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path =
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
;session.entropy_length = 16
;session.entropy_file = /dev/urandom
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 4
Every page calls session_start(); at the top
I am using the $_SESSION array to store my session variables
So when i navigate to index.php directly, my session info is there and I can access it using something like $_SESSION['logged']
When I navigate to the root webpage which loads index.php by default, the session variables are empty 2/3 of the time.
my php.ini has the following session entries:
[Session]
session.save_handler = files
;session.save_path =
session.use_cookies = 1
; session.use_only_cookies = 0
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path =
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
;session.entropy_length = 16
;session.entropy_file = /dev/urandom
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 4
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Session data disappears on main page
Well, you didn't really answer any of my questions... :/
According to you, everything should be fine, but something isn't. index.php has session_start() at the top, yet something $_SESSION doesn't exist? Or is it just empty? Do you end the session at any point when the user goes to the start page?
You may need to show us code in index.php, since that is the problem page.
According to you, everything should be fine, but something isn't. index.php has session_start() at the top, yet something $_SESSION doesn't exist? Or is it just empty? Do you end the session at any point when the user goes to the start page?
You may need to show us code in index.php, since that is the problem page.
Re: Session data disappears on main page
sorry if I am not getting you the info you need, I am new to the whole PHP arena.
the session variables are empty, like they are not initialized for the session. The only time the session is ended is if they click logout. As I mentioned, if I refresh the page repeatedly, sometimes the session variables have data, sometimes they are empty, but it only happens if index.php was loaded through the default method, and not called directly.
Here's the code:
the session variables are empty, like they are not initialized for the session. The only time the session is ended is if they click logout. As I mentioned, if I refresh the page repeatedly, sometimes the session variables have data, sometimes they are empty, but it only happens if index.php was loaded through the default method, and not called directly.
Here's the code:
Code: Select all
<?php
if (isset($_GET['loginerr']))
{
echo "<p class=error>Invalid login. Please try again.</p>";
}
if ($_SESSION['logged'] == true)
{
$welname = $_SESSION['uname'];
echo "<form action=logout.php method=post class=TopMenu><p class=TopMenu>Welcome back $welname! ";
?>
<input type="hidden" name="refpage" id="refpage" value="<?php echo $_SERVER['PHP_SELF'];?>" />
<input type="image" name="logoutsubmit" id="logoutsubmit" src="images/logout.png" class="submitbutton" />
</form>
</p>
<?php
}
else
{
?>
<form action="login.php" method="post" class="TopMenu">
<a class="TopMenu" href="join">Join</a> |
Login (Email) <input type="text" name ="email" maxlength="50" id="email" class="login">
Password <input type="password" name ="pass" maxlength="10" id="pass" class="pass">
<input type="hidden" name="refpage" id="refpage" value="<?php echo $_SERVER['PHP_SELF'];?>" />
<input type="image" name="loginsubmit" id="loginsubmit" src="images/login.png" class="submitbutton" />
</form>
<?php
}
?>
Re: Session data disappears on main page
oh, and before that code there is a session_start(); command, just forgot to paste it into the post.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Session data disappears on main page
And you can't find any pattern at all when $_SESSION is empty and when it isn't?
Try this at the start of index.php, just to see whats going on:
Try this at the start of index.php, just to see whats going on:
Code: Select all
echo 'ID: ' . session_id . "\n" . print_r($_SESSION);Re: Session data disappears on main page
When the session is there, I get this:
Array ( [logged] => 1 [uname] => Jason [uid] => 15428 ) ID: session_id 1
When the session is missing, I get this:
Array ( ) ID: session_id 1
On 5 reloads, I got the missing one 3 times and the populated one 2 times. I can't see any pattern to when it is empty and when it is not empty.
Array ( [logged] => 1 [uname] => Jason [uid] => 15428 ) ID: session_id 1
When the session is missing, I get this:
Array ( ) ID: session_id 1
On 5 reloads, I got the missing one 3 times and the populated one 2 times. I can't see any pattern to when it is empty and when it is not empty.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Session data disappears on main page
.. You're going to have to post some more relevant code then, because your error isn't showing in the code you've posted.
For example, where the session variables are set.
For example, where the session variables are set.