Home > Article > Backend Development > Detailed example code for php session use_PHP tutorial
session_start(), session_register(),session_unregister(),
session_is_registered(), session_destroy function.
session_start(): Enable the session mechanism and call it at the beginning of the program file that needs to use the session.
session_register(): Register session variable
session_unregister(): Delete session variables (one by one)
session_is_registered(): Determine whether the session variable is registered
session_distroy(): Destroy all session variables (all session variables are destroyed)
Initialize session
Example 1
*/
session_start();
//Delete all session variable values
$_session=array();
if(isset($_cookie[session_name()]))
{
setcookie(session_name(),'',time()-42000,'/');
}
//Log out all sessions
session_destroy();
/*
Example 2
*/
session_start(); //Initialize session
$a=5; //Define variables
session_register('a'); //Register session
session_decode("
print (session_is_registered('a')?$_session[a]:'not registered' ); //Determine whether the variable is registered
/*
Example 3
*/
/*Initialize session*/
session_start();
$_session['login_ok']=true; //Define session value
$_session['nome']='sica'; //Define session value
$_session['inteiro']=34; //Define session value
$result=session_encode(); //Encrypt the session value
echo $result; //Output result
session_destroy (); // Log out all session values
/*
Example 4
*/
session_start(); //Initialize session
$_session['name']="yoursession"; //Register a session variable
if(!isset($_session['initiated'])) //If a value is not set
{
session_regenerate_id(); //Get the current session id
$_session['initiated']=true; //Set value to true
}
session_destroy(); //Log out session
//Example 5
session_start(); //Initialize session
$_session['name']="yoursession"; //Register a session variable
if(session_is_registered(name)) //Judge
{
echo "The specified variable has been registered as session
"; //Output content
}
else
{
echo "The specified variable is not registered as session
"; //Output the corresponding results
}
session_destroy(); //Log out session
?>