Session Handling Help Required

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

Session Handling Help Required

Post by saadatshah »

///// Starting Session

session_start();

////// Registering Session Variables

$UserName="somethong";
$UserID="1";
$SessionID=session_id();
$SessionTime=time();

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

///// printing variables

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


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

when this page is accessed first time...it gives the following notices....when i refresh th page it print the values of the session variables.....

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

Notice: Undefined index: UserName in D:\test\admin.php on line 42

Notice: Undefined index: UserID in D:\test\admin.php on line 43

Notice: Undefined index: SessionID in D:\test\admin.php on line 44

Notice: Undefined index: SessionTime in D:\test\admin.php on line 45
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: Session Handling Help Required

Post by protokol »

Code: Select all

session_start();

   // Registering Session Variables

   $_SESSIONї'UserName'] = "somethong";
   $_SESSIONї'UserID'] = "1";
   $_SESSIONї'SessionID'] = session_id();
   $_SESSIONї'SessionTime'] = time();
   
   // printing variables	
			
   print($_SESSIONї'UserName']);
   print($_SESSIONї'UserID']);
   print($_SESSIONї'SessionID']);
   print($_SESSIONї'SessionTime']);
Basically, session_register() is not used anymore in the newer versions of PHP. Instead you can just access the $_SESSION array directly as I did above, by setting values to each of the variables.
saadatshah
Forum Newbie
Posts: 23
Joined: Fri Jul 05, 2002 5:50 am
Location: Lahore

Post by saadatshah »

thx .....
which version of php u were talking abt.....in ur eply
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Version 4.1 and up can use $_SESSION. If you had an older version you still need to use session_register() - AFAIK - and can access the variables using the $HTTP_SESSION_VARS array.

Mac
LenHewitt
Forum Newbie
Posts: 10
Joined: Sat Jul 06, 2002 6:13 am
Location: United Kingdom

Post by LenHewitt »

sadaatshah,

The problem with your dode is that you are registering the session variables after declaring them:

session_start();

////// Registering Session Variables

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


$UserName="somethong";
$UserID="1";
$SessionID=session_id();
$SessionTime=time();

will work
Post Reply