Session Management in the PHP4

Source: Internet
Author: User
Tags http request session id new features variables variable time limit access
The most notable of the new features of session PHP4 is the management function of the session. When someone visits your site, you can save variables to the session object, enabling many new features.

What is session

If you're not quite sure, let me explain what a session is. When someone visits your site, it starts at the end of his site. Of course, you can also terminate it in the program. Basically, cookies are associated with browsers, Some of the resources on the server are used to save the session's variables. By default, PHP4 saves these variables with a file. Of course, in theory, it can also be saved with a database or shared memory.

All Web pages that use the PHP4 session feature must call the Session_Start () function, allowing the PHP4 engine to load the session-related information into memory. Session_ The start function attempts to find the session ID from a cookie or a parameter of the current HTTP request. If not found, a new session object is created.

What is a session variable

The session variable is actually a normal global variable. When you register (declare) as a session variable, all Web pages that use the PHP4 session feature can access its value. To register a variable as a session variable, just assign the variable first, Then call Session_register ("variable name"). In subsequent pages using the Session function (via Session_Start), the variable name is not required to re-register as a session variable, it is like a normal variable, And its value is given to it in front. Change the value of this variable in the program, it will automatically re-register and save, and in its subsequent web pages can be used normally.

So, what can it do?

Good question! There are many ways to use Session management and session variables. Here, I give an example to illustrate. If you are building a site for a virtual community, you may want to track the current proven username and how many new messages he has. To reduce the load on the database, You have to save this information in other ways. There are two ways to do this:

Use three cookies
Authenticatied_user-Current Proven user name
Num_message-How much new information does he have?
Expiration time limit for Expire_time-cookie
Use session to register three session variables
The first method has a large security risk, others can forge cookies, and then use another account to access the system. Again, all information is implemented through the header () function, which is more cumbersome. In addition, when a user's browser refuses to accept one of the cookies, the entire data is incomplete.

With session, as long as a cookie, you can save all the information. Security is better.

The inadequacy of the place

With any server-side scripting language, sessions can provide a very free, flexible, and powerful feature. But there are some limitations to the session in PHP4:-You can't save objects (Objects) itself in session- The data saved in the session is not efficient because PHP4 uses a file to hold the session information.
 



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.