More Help on Sessions Needed

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
saadatshah
Forum Newbie
Posts: 23
Joined: Fri Jul 05, 2002 5:50 am
Location: Lahore

More Help on Sessions Needed

Post by saadatshah »

PHP Version 4.0.6 on Linux and Apache

****** file1.php

//// starting session

session_start();


//// registering session variables

session_register("UserName,UserID,SessionID,SessionTime");

$_SESSION['UserName'] = $txtLogin;
$_SESSION['UserID'] = 1;
$_SESSION['SessionID']= session_id();
$_SESSION['SessionTime']= time();


//// printing session variables

print($_SESSION['UserName']);
print($_SESSION['UserID']);
print($_SESSION['SessionID']);
print($_SESSION['SessionTime']);


end of file1.php*************************************



********file2.php


//// starting session

session_start();

//// printing session variables

print($_SESSION['UserName']);
print($_SESSION['UserID']);
print($_SESSION['SessionID']);
print($_SESSION['SessionTime']);

****end of file2.php


Note:-----
The first file register and prints the session variables but ...the second file doesn't .....can anyone help me...point out what is the mistake in my code......v r running PHP 4.0.6 on a LINUX sever with APACHE Web server.......... and register_global is on....so i need ur quick help...
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

It worked fine for me (I think). I'm using PHP 4.2.1. The problem must be that you're using 4.0.6.

According to the PHP documentation, if I understand correctly, the $_SESSION variable is not a global variable until 4.1.0--
Note: As of PHP 4.1.0, $_SESSION is available as global variable just like $_POST, $_GET, $_REQUEST and so on. Not like $HTTP_SESSION_VARS, $_SESSION is always global. Therefore, global should not be used for $_SESSION.

http://www.php.net/manual/en/ref.session.php
I'm not sure how to get around this problem--since I've never used session functions--so you may want to read Session handling functions in the PHP docs.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

As gnu2php pointed out, $_SESSION only works in PHP 4.1 and above so not in 4.0.6. For that you'll need $HTTP_SESSION_VARS and session_register.

Why don't you have a look at the tutorial on sessions at phpcomplete.com.

Mac
saadatshah
Forum Newbie
Posts: 23
Joined: Fri Jul 05, 2002 5:50 am
Location: Lahore

Post by saadatshah »

hi,
i ve replaced the $_SESSION with $HTTP_SESSION_VARS in my code...but this iz still not workin......the article u suggested ...i ve already read that and the documentation 2.....but cant fix my problem.....any more sugesstions?????
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Apologies, but having just reread the documentation for session_register() I noticed the following caution:
php manual wrote:If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
I would therefore amend your code by removing the session_register() statement and convert all $_SESSION references to $HTTP_SESSION_VARS. Otherwise use session_register() in place of $HTTP_SESSION_VARS, it appears to be that you should choose one or the other and not use both.

Mac
saadatshah
Forum Newbie
Posts: 23
Joined: Fri Jul 05, 2002 5:50 am
Location: Lahore

Post by saadatshah »

************ file1.php

<?
//// starting session

session_start();



$HTTP_SESSION_VARS['UserName'] ="Test";
$HTTP_SESSION_VARS['UserID'] = 1;
$HTTP_SESSION_VARS['SessionID']= session_id();
$HTTP_SESSION_VARS['SessionTime']= time();



//// printing session variables

print($HTTP_SESSION_VARS['UserName']);
print($HTTP_SESSION_VARS['UserID']);
print($HTTP_SESSION_VARS['SessionID']);
print($HTTP_SESSION_VARS['SessionTime']);



/* when session_register() is used

//// registering session variables

//session_register("UserName,UserID,SessionID,SessionTime");


//// printing session variables



print("<br>" . $UserName);
print("<br>" . $UserID);
print("<br>" . $SessionID);
print("<br>" . $SessionTime);

*/
?>

******************

********** file2.php

<?
//// starting session

session_start();

//// printing session variables

print($HTTP_SESSION_VARS['UserName']);
print($HTTP_SESSION_VARS['UserID']);
print($HTTP_SESSION_VARS['SessionID']);
print($HTTP_SESSION_VARS['SessionTime']);



/* when session_register() is used

print("<br>" . $UserName);
print("<br>" . $UserID);
print("<br>" . $SessionID);
print("<br>" . $SessionTime);
*/

?>

**********************************

hello guy.....this is my new code but this is also not workin....now what??????????
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You are still using session_register() and $HTTP_SESSION_VARS together. Try using just one or the other.

Mac
saadatshah
Forum Newbie
Posts: 23
Joined: Fri Jul 05, 2002 5:50 am
Location: Lahore

Post by saadatshah »

hi,
i think u didnt take a close look at the code...the sessio_register(); portion is commented in my code....
Post Reply