Home > Backend Development > PHP Tutorial > Detailed example code for php session use_PHP tutorial

Detailed example code for php session use_PHP tutorial

WBOY
Release: 2016-07-13 16:55:10
Original
878 people have browsed it

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(""); //Decrypt string
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
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631714.htmlTechArticlesession_start(), session_register(), session_unregister(), session_is_registered(), session_destroy function. session_start(): enable The session mechanism is the most important program file that needs to use session...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template