Application of Session in PHP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.