Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for deprecated session_is_registered

Tags:

php

session_start();
if (!session_is_registered(user)) {
    header("Location: login.php");
    die();
}

What is the proper way to do this since session_is_registered() is deprecated?

like image 218
Kyle Avatar asked Mar 12 '11 23:03

Kyle


3 Answers

if (empty($_SESSION['user'])){
    // session user does not exist
}
like image 51
Yasen Avatar answered Nov 01 '22 13:11

Yasen


use if ( isset( $_SESSION['user'] ) ){}

like image 45
Galen Avatar answered Nov 01 '22 15:11

Galen


isset() will not do what you want all of the time. It will fail if 'user' is falsy (0, false, null, empty string). array_key_exists() will match the truth table of session_is_registered, despite what php.net says:

if ( !array_key_exists( 'user', $_SESSION ) ){
  /* 'user' is in the session */
}

//in other words...
if ( array_key_exists( 'user', $_SESSION ) === session_is_registered( 'user' ) ) {
  /* This is always executed */
}
like image 1
Vectorjohn Avatar answered Nov 01 '22 14:11

Vectorjohn