Page 1 of 1

session_name() causes variables to be lost?

Posted: Fri Feb 10, 2006 2:52 pm
by mfindlay
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am maintaining a php site that makes us of session_name() to name sessions before starting them. On the live server it works fine, but on my local test server it fails to find session variables that were previously set.

The live server is running PHP 4.4 on FreeBSD and my test server is running PHP 5.0.3 for Windows on Win2K if that makes any difference.

The existing code takes the usual care to write the session vars before redirecting by using the session_write_close(); command. The problem appears to be the use of the session_name function. When I comment-out the use of session_name(), the session variable is found.

Code: Select all

$userip = $_SERVER['REMOTE_ADDR'];
$uip = str_replace('.','',$userip); // Gets user ip and removes dots. This will be our session name.

//**********************
// SetUser
//**********************
function SetUser($usr)
{
    session_name($GLOBALS['uip']);
    session_start();

    $_SESSION['username'] = $usr;
    session_write_close();
    header("Location: http://. . . ");  // redirect to target page. Target page called ValidateSessionUser() below
    exit;
}

//**********************
// ValidateSessionUser
//**********************
function ValidateSessionUser()
{
    session_name($GLOBALS['uip']);
    session_start();
    $query = "SELECT * FROM users WHERE username ='". $_SESSION['username'] . "'"        // username is empty here!
    . . .
}
So - on my test platform (Win2K Server, PHP 5.0.3) it fails unless I comment-out the session_name() function.

I can only assume it has to do either with my site configuration, or differences inherent in PHP versions, etc., since this works fine on the PHP 4.4 site.

Any thoughts on what could be causing this? I'm sort of dead in the water until I can resolve this. Please let me know if you need any further info regarding my configuration etc.

Thanks!


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Feb 10, 2006 4:53 pm
by hawleyjr
Why are you calling session_name()? It's not necessary.