PHP _SESSIONs Bug

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibben
Forum Newbie
Posts: 5
Joined: Sun Apr 15, 2007 4:26 pm

PHP _SESSIONs Bug

Post by ibben »

Everah | Edited to spell out the word you.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
hey all,
i am making use of php $_SESSION[]; the problem i am having that i cannot find any solid documentation on is the session variables are randomly being destroyed or reset. Randomly moving from page to page or reloading the same page all the session variables i set on the site will be reset. Any ideas on what may be causing this? is this a server thing, is it my browser; let me know what any of [s]u[/s] you would need to trouble shoot this with me.. thanks in advance...

ibben
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you getting a session cookie? Are you calling session start before output on every sessioned page? When you say the session data is reset do you mean the session array is empty?
ibben
Forum Newbie
Posts: 5
Joined: Sun Apr 15, 2007 4:26 pm

problem clarification

Post by ibben »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i am using session cookies, but not using $_COOKIE[], i'm using $_SESSION[]

Yes, i am including the same sessioncheck.php file at the very top of every page:

Code: Select all

<?php
session_start();

require "./includes/db_connect.inc";

if (isset($_SESSION["userName"])){
	$me = $_SESSION["userName"];

	$query = "SELECT * FROM users WHERE email = '{$me}' ";
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_array($result);

	$firstName = $row['firstName'];
	$lstName = $row['lastName'];
	$permissions = $row['permissions'];
}
?>

by session data reset, i mean that all the session variables and arrays that i set no longer have values. the above code makes use of a single userName; i also have arrays I'm using for items in a shopping cart. all the variables and session arrays i use go empty =="" at the same time. and whats weird is how random it is, i could reload the same page a few times and it'll reset...

Help!!! the sooner i can get this resolved the better!!!!

:?: :?: :?:


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Save the following two scripts as session-test-page1.php and session-test-page2.php. Place them on the server you are having trouble with. Load page1 then follow the links posting back what is echoed on each page please.

session-test-page1.php

Code: Select all

<?php
//Session start
session_start();

//Set session vars
if (!isset($_SESSION['username']) && !isset($_SESSION['password']))
{
    $_SESSION['username'] = 'testusername';
    $_SESSION['password'] = 'testpassword';
    
    //header('Location: session-test-page2.php');
    //exit;
}
else
{
    // Quick error checking
    echo '<pre>'; var_dump($_SESSION); echo '</pre>';
    echo '<p>You user name is already set to ' . $_SESSION['username'] . '</p>';
    echo '<p>And your password is already set to ' . $_SESSION['password'] . '</p>';
}

if (isset($_GET['clear']))
{
    // Unset all of the session variables.
    $_SESSION = array();

    // If it's desired to kill the session, also delete the session cookie.
    // Note: This will destroy the session, and not just the session data!
    if (isset($_COOKIE[session_name()])) {
        setcookie(session_name(), '', time()-42000, '/');
    }

    // Finally, destroy the session.
    session_destroy();
    
    echo '<p>Even though you see the information above, the session has already been terminated. 
    <a href="' . basename($_SERVER['SCRIPT_FILENAME']) . '">Refresh the page</a> and you will see nothing as the page sets the session vars again.</p>';
}

echo '<p><a href="session-test-page2.php">Click here for page 2</a></p>';
echo '<p><a href="?clear=true">Click here to clear the session data</a></p>';
?>
session-test-page2.php

Code: Select all

<?php
//Session start
session_start();

// Quick error checking
echo '<pre>'; var_dump($_SESSION); echo '</pre>';

// Echo session vars
echo '<p>You user name is ' . $_SESSION['username'] . '</p>';
echo '<p>And your password is ' . $_SESSION['password'] . '</p>';

echo '<a href="session-test-page1.php">Click here to go back to page 1</a>';
?>
ibben
Forum Newbie
Posts: 5
Joined: Sun Apr 15, 2007 4:26 pm

Post by ibben »

Thanks for the test files and quick reply: here is what I'm getting in order, using the "Click here for page#" links

session-test-page2.php
Click here for page 2

Click here to clear the session data
session-test-page2.php
array(2) {
["username"]=>
string(12) "testusername"
["password"]=>
string(12) "testpassword"
}

You user name is testusername

And your password is testpassword
Click here to go back to page 1
session-test-page1.php
array(2) {
["username"]=>
string(12) "testusername"
["password"]=>
string(12) "testpassword"
}
You user name is already set to testusername

And your password is already set to testpassword

Click here for page 2

Click here to clear the session data
session-test-page2.php
array(2) {
["username"]=>
string(12) "testusername"
["password"]=>
string(12) "testpassword"
}

You user name is testusername

And your password is testpassword
Click here to go back to page 1
I went back and forth a couple times like that between page 1 & 2 and the array stayed populated, but i eventually i got this::

session-test-page2.php
array(0) {
}

You user name is

And your password is
Click here to go back to page 1
any idea?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Man, that is tricky. What were the circumstances leading up to the clearing of the session array? Specifically, how did you wait before loading that page that showed empty? Did you move around very fast from click to click? Etc...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Is this a shared server? Is it possible something keeps cleaning out /tmp ?
ibben
Forum Newbie
Posts: 5
Joined: Sun Apr 15, 2007 4:26 pm

another

Post by ibben »

i'm checked with my hosting company ((mt)) about the shared server issue, they directed me to this page:
http://kb.mediatemple.net/article.php?id=235


the circumstances vary, but I've generally waited for each page to completely load before clicking to go to the next page...
ibben
Forum Newbie
Posts: 5
Joined: Sun Apr 15, 2007 4:26 pm

YAY

Post by ibben »

that shared server issue was the problem; so the solution was to edit the sessions save path in the php.ini ...

session.save_path = /home/####/data/tmp

thanks for your help everyone! really :!:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sucks that you had to go through it, but I am glad we were able to help.
Post Reply