How to start session sessions in PHP

Source: Internet
Author: User
The session setting is different from the Cookie and must be started first, and the Session_Start () function must be called in PHP to allow the PHP core program to preload the built-in environment variables associated with the session into memory.

Use the Session_Start () function to start a session with the following syntax format:

Session_Start (void);   Create session  , start a conversation, perform session initialization

Session_Start () This function has no parameters and the return value is TURE. The function has two main functions, the ritual begins a session, but returns the already existing session.

When the site is first visited, the Session_Start () function creates a unique session ID and automatically saves the session ID to the client Cookie via the HTTP response header. At the same time, the page creates a file named after this session ID on the server to hold the user's session information. When the same user accesses this website again, the session ID saved in the client Cookie is automatically brought over by the request header of HTTP, then the Session_Start () function will not allocate a new session ID. But in the server's hard disk to find and the session ID of the session file with the same name, the previously saved sessions for the user read out, in the current script to apply, to track the purpose of this user. So during a session, the same user accesses any page on the server using the same session ID.

Note: Typically, the session_start () function is called at the start of the page, and then the session variable is logged into the data $_session.

Note: If you use a Cookie-based session, you cannot have any output content until you use the function to open the session. Because the cookie-based session is actually turned on, calling the Session_Start () function generates a unique Session ID that needs to be stored in the cookie of the client computer, so use Session_Start () Function before the browser can have no output, even if it is a blank space and a blank line, it will produce an output string error.

If you do not want to use the Session_Start () function in each script to open the session, you can then php.ini inside the Session.auto_start = 1, you do not need to call the session before each use of Session_Start () function. But this option is also hungry for oil some limitations are that you cannot put an object in the session because the class definition must be loaded before the session starts. Therefore, it is generally not recommended to use Session.auto_start to open the Session.

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.