PHP Session application entry

Source: Internet
Author: User
Compared with Cookie, Session is stored on the server, which is relatively secure and has a storage length limit as the Cookie does. This article briefly introduces the application of Session first. The Session is stored on the server as a text file, so no

Compared with Cookie, Session is stored on the server, which is relatively secure and has a storage length limit as the Cookie does. This article briefly introduces the application of Session first.

Because the Session is stored on the server as a text file, the client is not afraid to modify the Session content. In fact, PHP takes the initiative to modify the Session file permission in a Session file on the server. it only retains the system read and write permissions and cannot be corrected through ftp, which is much safer.

For Cookie, if we want to verify whether the user logs on, we need to keep the user name and password (which may be the md5 encrypted string) in the Cookie and verify it every time on the page. If the user name and password are stored in the database, a database query is performed every time, causing excessive burden on the database. We cannot perform only one verification. Why? The client Cookie information may be corrected. If you store the $ admin variable to indicate whether the user logs on, $ admin logs on when it is set to true, and false does not, after the first verification is passed, $ admin is stored as "true" in the Cookie, so no verification is required next time. is this correct? Wrong. if someone fabricated a $ admin variable with a value of true, didn't the administrator privilege be obtained immediately? Very insecure.

The Session is different. The Session is stored on the server, and the remote user has no way to modify the content of the Session file. Therefore, we can simply store a $ admin variable to determine whether to log on, after the first verification is passed, set $ admin to true, and then determine whether the value is true. if not, switch to the login interface, which can reduce the control of many databases. In addition, it can reduce the security of passing passwords to verify cookies every time (Session verification only needs to be passed once, if you have not applied the SSL security protocol ). Even if the password is encrypted with md5, it is easily intercepted.

Of course, the application Session has many advantages, such as easy control and user-defined storage (stored in the database ). I will not talk about it here.

Does Session need to be set in php. ini? Generally, this is not required because not everyone has fixed php. ini permission. The Default Session storage path is the temporary system folder of the server. we can customize it to be stored in our own folder. this will be done later.

Start with how to create a Session. Very simple, really.

Start the Session and create a $ admin variable:

// Start the Session
Session_start();
// Declare a variable named admin and assign a null value.

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.