Session and Cookie Understanding in PHP

Source: Internet
Author: User
Tags change settings php session setcookie unique id

The PHP session variable is used to store information about user sessions, or to change settings for user sessions. The session variable holds information that is a single user and is available to all pages in the application.

Understanding: Session for a single user and server interaction, each user's session is not the same. Session as a global variable, valid throughout all pages during browser opening.

The session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.

Before you store the user information in the PHP session, you must first start the conversation.

Note: the Session_Start () function must precede the

Can be used to facilitate the interaction between pages, you can put the array into session. More useful than post, afraid of seesion too much will affect performance AH.

If you want to delete some session data, you can use the unset () or Session_destroy () function.

The unset () function frees the specified session variable; the Session_destroy () function completely ends the session.

**********************************************************************************

Cookies are often used to identify users. Cookies are small files that the server leaves on the user's computer. Whenever the same computer requests a page through a browser, it sends cookies as well. With PHP, you can create and retrieve the value of a cookie.

Understanding: Cookies are stored on the user's computer and are more convenient for storing user information.

Syntax: Setcookie (name, value, expire, path, domain);

Example: Setcookie ("User", "Alex Porter", Time () +3600); Looks like it's an hour for Alex. The back time parameter is the due date

echo $_cookie["user"];

Note: When the cookie is sent, the value of the cookie is automatically URL coded and automatically decoded when it is retrieved

When you delete a cookie, you should change the expiration date to a past point in time.

Setcookie ("User", "", Time ()-3600);


You can also use the following understanding:


1.cookie is what.

A cookie is data (usually encrypted) that some Web sites store on a user's local terminal in order to identify the user and perform session tracking.

Popular to understand is that you go to a store or supermarket to buy things, and then the store for you to do a membership card, your identity and purchase information are present in this card, and this card put you. Every time you go shopping, you just have to swipe your card, and you don't have to register or record any other information.

then this paragraph map of the Web, supermarket checkout desk is the server side, and you are the client, you have the card is the existence of a client in the cookie file, which records your account password and other information.

but to be aware of, cookies in the second use of the time to be effective, that is, the first time you buy things in the supermarket, people will give you a card, you can buy it after the card, but the first time you buy, the supermarket did not have any information about you, so the first time you do not have a card. Web site is the same, the first time to login to a website, of course, to enter the account password and other information, and then to generate cookies exist locally, so that the next time to use.

at the same time, cookies have their own validity period, after the expiration of the period, the local cookie file will be automatically deleted. You need to log in again, enter your account password, and then generate a new cookie. The main purpose of this is still for security reasons. 2.cookie mechanism diagram.

3.cookie use method.

(1) Set Cookies

[PHP] view plain copy bool Setcookie (string $name, $value, $expire, $path, $domain, $secure, $httponly for example:

[PHP] view plain copy Setcookie ("username", "user", 0, "/"); Setcookie ("username", "user", Time () +60*60, "/");

the use of each parameter is not explained. Here we will focus on the time and path of setting cookies in the above two ways.

the first one of the time put a 0 in, does not represent the survival time of 0. Obviously, it has a special meaning, indicating that the expiration of the cookie ends with the browser closing. They have a "/" in their path. This means that all the Contentpath in this domain can access the cookie, which means that all pages under the site can track the cookie.

(2) Delete Cookies

[PHP] view plain copy Setcookie ("username", "", Time ()-3600, "/"); and set cookies a thing, that is, the value of the cookie is not, the time is earlier than the current time, and then does not expire.

(3) View Cookies

[PHP] view plain copy print_r ($_cookie); This guy must know all about it, just wasting your eyes.

----------------------------------------------------------------------------------

-----------------------------I'm a split line-------------------------------------------

---------------------------------------------------------------------------------- 1.session is what.

Session refers to the time interval between a terminal user communicating with an interactive system, usually the time elapsed between registering into the system and logging off from the system.

The working principle of session (from Baidu) (1) When a session is first enabled, a unique identity is stored in a local cookie. (2) First using the Session_Start () function, PHP loads the session variables that have been stored from the session warehouse. (3) When executing a PHP script, register the session variable by using the Session_register () function. (4) When the PHP script executes, the unsaved session variable is automatically saved in the session library under a local path, which can be specified by the Session.save_path in the php.ini file and can be loaded the next time the page is browsed.
In fact, the popular word is that you go to the supermarket to buy things, the membership card records your information, but the membership card is not kept in your place, but the way the data exist in the supermarket system, once registered can be used directly. You can use it directly when you need it. But once you leave the supermarket, the membership card will lose its usefulness until your next purchase. At the same time, the only identification of the membership card is yourself, no one else can use your membership card. The direct condemnation is very well understood.
A big difference between the session and the cookie is that the session is registered directly after use, that is, the first purchase can be used, and cookies are after the first purchase of information into the membership card, and then the second time to start using.

2.session mechanism diagram.

3.session use method.

(1) Set session

[PHP] view plain copy session_start ();
[PHP] view plain copy $_session[' username ']= ' user ';
You need to open the session every time before you use the session, just as you usually need to open the door first. And in the setting session and the variable to assign the value of not much difference, in fact, $_session itself is a variable.

(2) Delete session

This relative step is a bit more than a word in the cookie box.

[PHP] view plain copy

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.