First, what is the session
Sessions are similar to cookies in order to maintain a session, that is, to remember the information of the client user.
Ii. features of the session
1.session saved user information is saved on the server side and not on the client browser.
2.session still needs to leave a unique tag on the client SessionID, the user's information is saved on the server. There are two ways to base a cookie or URL
Third, the use of a cookie-based session
Configuration of 1.session
Registered session_start (); ---> cookie-based session needs to be preceded by HTML at the beginning of the script
Session_Start (); ---> After the session, when the user first visits the site, the server will default to the browser to send a cookie message named SessionID
The steps to send a cookie to the browser have been omitted.
2. Use the value of session in the script: first open session_start (); You can then define or use session values in other scripts
Session_Start ();
$name = $_session[' name '];
$_session[' age '] = 20;
echo $_session[' fuck '];
3. Logout session
//Open SessionSession_Start();//Remove SessionID that are reserved on the browserif(isset($_cookie[Session_name()])){ Setcookie(Session_name(),‘‘, Time()-3600, '/'); {//clears the global session variable$_session=Array();//clears the session file that is reserved on the serverSession_destroy();
Iv. use of the URL-based session
1. Open session
Session_url ();
Session_Start ();
2. Because the SessionID is passed through the URL, so on the page all need user information to add after the link? sid=<?php echo session_id ();?>
3. Delete session information, simply remove the server-side
V. Setting automatic selection of Session based on cookie or URL
1. Find configuration in Apache Session.use_trans_sid = 1//Determine if the browser is turning on cookies, and if not, automatically add a suffix with session_id () to all connections or form submissions in the script
2. Open session_start () when used;
3. In the script if there is output as a JavaScript connection form, you cannot jump, you need to add the SID after the link
Application of Session in PHP