PDA

View Full Version : Registering POST/GET variable as session variable?


WA
12-13-2002, 12:19 PM
Hi:
In PHP4.2+, I was wondering what the "standard" technique is to registering a post/get variable as a session variable, for example a username entered by the user to be shown on all other pages of site. The fact that the variable must now be assessed via an array is where my dilemma comes in. Doing something like:

session_register("$_POST['username']");

doesn't work, both syntactically and logically; for the later, since the above registers a POST variable, one which may never have been entered in the first place by the user if you calls up a page without the login form.

I guess what I'm asking is, is the technique in PHP4.2 to always save a POST variable as a local variable first, then register it? In other words, there's no way to cut a step and directly register a POST variable and use it elsewhere on the site?

Thanks,

firepages
12-13-2002, 01:17 PM
$_POST['name'] = 'bella';session_register($_POST['name']) ;

creates the session variable $_SESSION['bella']; which is empty


<?
session_register('username');
$_SESSION['username']=$_POST['username'];
?>


you can do this ...session_register($_POST); which registers all _POST variable as session varaibles, and then import them into the session array if you wish i.e.


<?
session_register($_POST);
$_SESSION=$_POST;

//test it//
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>

mordred
12-13-2002, 02:14 PM
Hmh. Directly from the PHP manual (http://www.php.net/manual/en/function.session-register.php), I found:


Caution:

If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().


From this I take that you should either decide for using session_register or directly storing the variables in $_SESSION, either one. Although the manual stays a bit unclear about the reasons for doing so.

I would store the $_POST value in the session like this:


session_start();
$_SESSION['username'] = $_POST['username'];


and read it out like


session_start();
var_dump($_SESSION);


Works for me...

ConfusedOfLife
12-13-2002, 05:06 PM
Check this out:
http://www.codingforums.com/showthread.php?s=&threadid=10986