tags:

views:

632

answers:

3

Hi all, does anyone knows the alternative to the deprecated function session_is_registered in PHP5?

here's my code:

ob_start();

session_start();

if(!session_is_registered(myusername))

{

    header("location:main_login.php");

}

ob_flush();

Thanks,

Mauro

+4  A: 

"You need to set and reference $_SESSION variables only." For example:

if( isset($_SESSION[$myusername]) )

From http://www.phpfreaks.com/forums/index.php?topic=263189.0

zaf
Please cross check with Mr Sirber's answer. But don't forget to upvote and accept my answer anyway because without my spectacular answer he would not have had thought of his small tip.
zaf
+2  A: 

on a side note, best use $_SESSION['username'] = $myusername;. Using the $_SESSION[$myusername] as a variable may overwrite existing variables in the session.

Sirber
I could have sneakily changed my post but I won't. @Mauro74 Accept my answer but note this guys tip and give him an upvote - he deserves it for spotting my silly mistake. I'll upvote you as well - as long as you upvote me and all my questions/answers on stackoverflow and we shall overtake this site and topple Mr Skeet.
zaf
zaf: thank you!
Sirber
A: 

Use session_id()

if (!session_id()) {
 // do stuff
}

if there is no session id nothing will be returned. (docs - session_id() )

RDL
you get a session_id() as soon as you call session_start(); the OP wants to see if a user is logged in.
Sirber