[PHP] advanced tutorial: create and use Cookies and Sessions in PHP

Source: Internet
Author: User
Tags php session
Cookies: Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. PHP allows you to create and retrieve cookies. How to Create a cookie? The setcookie () function is used to set the cookie. Syntax setcookie (name

Cookies: Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. PHP allows you to create and retrieve cookies. How to Create a cookie? The setcookie () function is used to set the cookie. Syntax setcookie (name

Cookies:

Cookies are often used to identify users. Cookie is a small file that the server stays on the user's computer. When the same computer requests a page through a browser, it sends a cookie at the same time. You can use PHP to create and retrieve the cookie value.


How to Create a cookie?
The setcookie () function is used to set the cookie.

Syntax
setcookie(name, value, expire, path, domain);
Example
In the following example, we will create a cookie named "user" and assign it "Alex Porter ". We also stipulate that the cookie will expire in one hour:
 
Note: When sending a cookie, the cookie value is automatically URL encoded and automatically decoded during retrieval (to prevent URL encoding, use setrawcookie () instead ).



How can I retrieve the Cookie value?
The $ _ COOKIE variable of PHP is used to retrieve the cookie value.
In the following example, we retrieve the cookie value named "user" and display it on the page:
 

In the following example, we use the isset () function to check whether the cookie has been set:
 ";else  echo "Welcome guest!
";?>




How to delete a cookie?

When deleting a cookie, you should change the expiration date to the past time point.
Example of deletion:
 

What if the browser does not support cookies?

If your application involves a browser that does not support cookies, you have to use other methods to pass information from one page to another in the application. One way is to pass data from the form (the content about the form and user input is described earlier in this tutorial ).

The following form submits user input to "welcome. php" when the user clicks the submit button:
 

Retrieve the value in "welcome. php" as follows:

Welcome 
 .
You are years old.



Session:

The PHP session variable is used to store information about a user session or change the settings of a user session. Session variables are stored by a single user and can be used on all pages of the application.


PHP Session variable
When an application is running, it is opened, changed, and closed. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.
By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database.

The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL.


Start PHP Session
Before storing user information in a PHP session, you must start the session.

 

The above code registers a user's session with the server so that you can start to save user information and assign a UID to the user session.
Store Session Variables

The correct method for storing and retrieving session variables is to use the PHP $ _ SESSION variable:
 
 

Output: Pageviews = 1


In the following example, we create a simple page-view counter. The isset () function checks whether the "views" variable has been set. If the "views" variable has been set, we accumulate the counter. If "views" does not exist, create the "views" variable and set it to 1:
 

End Session

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

The unset () function is used to release the specified session variable:
 
You can also use the session_destroy () function to completely terminate the session:
 

Note: session_destroy () resets the session and loses all stored session data.

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.