session_register depricated

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
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

session_register depricated

Post by inosent1 »

i have been using session_register to pass along the username to the various pages of my data system. i remember it took me forever to figure out how to do that. i like it because as i go from page to page while logged in i can use a query based on the username and do useful things with it

in php 5.3+ i get the 'deprecated' error messages which are annoying. so what i am looking for is a way to register the username variable just like i did with session_register

the login page:

Code: Select all

$username = count($_POST)?$_POST['username']:'';
session_register("username");
$_SESSION['user']=$username;

then all the following pages

Code: Select all

 session_start();
// to see if the user is logged in
if(!session_is_registered(username)){
header("location:login.php");
}


$usera = $_SESSION["user"];
//echo 'here ' . $usera;
 
so i need a way to do what session_register used to do, as easily as it did.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: session_register depricated

Post by requinix »

What you actually need is to not use session_register() and session_is_registered(). Those deprecation warnings aren't there to make your life harder: they're there to tell you what not to do.

Use $_SESSION. To "register" a variable just stick it in the array, like you're doing now, and to check if one "is registered" use isset with the array.
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

Re: session_register depricated

Post by inosent1 »

thanks. it was easier than i thought

login

Code: Select all

$username = count($_POST)?$_POST['username']:'';
$Session = $_SESSION["Session"];
$_SESSION['user']=$username;		


subsequent pages

Code: Select all

<? session_start();
if(!$_SESSION["user"]){
header("location:login.php");

?>
Post Reply