Research of session and Cookie in PHP

Source: Internet
Author: User
Tags setcookie

First, Session

(1) The Origin and introduction of the session

Session: In the computer, especially in the network application, called "Session Control", the time to live for the user when browsing a website, from entering the site to close the site after the time elapsed, that is, users to browse the site spent time.

Because HTTP is a stateless protocol that is only responsible for requesting the server, it loses contact with the browser when it is corresponding to the server. Can not save the user's personal information, like a shopping mall and a vending machine or the relationship between ordinary people, so in order to compensate for this shortcoming session only answered, session needs in 4.1 version above

(2) The mechanism of the session

The session mechanism adopts the scheme of maintaining state on the server side, and has its own unique identifier session_id, which distinguishes the information before different users.

For example: We go to a restaurant to eat, we have a membership card, it has my personal information, as well as the effective time, when I come every time the restaurant can find my personal information

(3) How the session works

1> when a session is first enabled, a unique identifier is stored in a local cookie.

2> first uses the Session_Start () function, and PHP loads the stored session variables from the session repository.

3> when executing a PHP script, register the session variable by using the Session_register () function.

4> when the PHP script executes, the non-destroyed session variable is automatically saved in the session library under the local path, which can be specified by Session.save_path in the php.ini file and can be loaded the next time the page is browsed.

Saved files:

More information:

(4) What is the application of the session project in actual combat?

1> Logging User login information, global use

2> Make verification Code, verify contrast operation

3> save session to database can be implemented: a. Controlling an account can only be logged on by one person

(5) Advantages and disadvantages of Session

Advantages of 1>session: uniqueness, convenient invocation, no excessive resource consumption, is a set of temporary data stored on the server. In general, when we do the user login, we will save the user data to the session. This makes it easy to invoke on any page, and each client produces a unique session_id that does not mix. And after the browser is closed, the server will have a session recovery mechanism, automatically delete the expired session

2> Cons: Cookies are disabled when the client is stored in a cookie.

(6) connection and difference between session and Cookie

1> Contact: Session in the client also need to save an identity, so the use of cookie,session is through the cookie to work between the session and the cookie is through $_cookie[' PHPSESSID '] to contact, through the $ _cookie[' Phpsessid ' can know the ID of the session to get other information.

2> differences: The cookie mechanism uses a scheme that maintains state on the client (browser), while the session mechanism uses a scenario that maintains state on the server side

(7) Use of Session

Sesstion_start (); First Open session

$_session[' user '] = ' username '; Put username in there.

echo $_session[' user '; Direct output username

Session_destroy (); Destroy session

Second, cookies

(1) Origin and introduction of cookies

Cookies are produced as a product of the Internet, and are also intended to preserve some basic information about the user, as well as information that the server temporarily saves on your computer, and the next time you come, look at the information, and then load the data directly and send out specific information.

(2) Mechanism of cookies

Cookie is the mechanism of the session is similar, all will be generated for each user a specific value, this time is stored in the client, when we open the cookie stored our personal information site, he will submit our personal information to the server, and then request the server corresponding information

(3) The principle of cookies

When you visit a website, you can save some small amount of data on the server to the hard disk, such as user name, ID, etc., and so on your next visit, the server will first look for this file and then read your relevant information, so that you can not enter the password of the user name directly to the site

(4) What are the applications of cookies in the actual project?

1> Remember user name password

(5) Advantages and disadvantages of cookies

1> advantages: A. Server resource space is not required B. longer duration

2> Disadvantages: A. Client size restricted B. User-disabled cookies then it's useless. C. unsafe

(6) Differences and links between cookies and session

1> Contact: Cookies can save a unique identifier for the session at the client

2> differences: The cookie mechanism uses a scheme that maintains state on the client (browser), while the session mechanism uses a scenario that maintains state on the server side

(7) Use of cookies

1> creation and update: Setcookie ($cookieName, $value, time () + seconds);

Example: Setcookie ("UserName", "Imooc", Time () +2*7*24*3600);

Value: $uName =$_cookie[' UserName '];

2> Delete Cookies

Setcookie ($cookieName, Value,time ()-seconds);

Or

Setcookie ($cookiename, ");

Or

Setcookie ($cookiename, NULL);

Example: Setcookie ("UserName", "Imooc", Time ()-3600);

Research of session and Cookie 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.