PHP Cookie Learning Note _php Tutorial

Source: Internet
Author: User
Tags set cookie setcookie
A cookie is a session that is used in all programs, and below I share my PHP cookie notes to fellow students, this article introduces all the tips on how to get started with cookies, as you can see from the students who need them.

What does a cookie mean?

A cookie is used to store a user name, password, number of times to access the site, and so on. When visiting a website, a cookie sends an HTML page to a small piece of information in the browser, which is saved as a script on the client's computer.

What is the use of cookies? If the server to record the number of users on the station, then the long-term accumulation of data must be very large, the burden on the server. Therefore, the data can be stored on the user's own computer, and when needed, the server then reads the cookie extraction data on the user's computer, so that the server does not need to record a large amount of data.


Generally, cookies are returned to the browser from the server side via HTTP headers. First, the server side uses the set cookie header in the response to create a cookie. The browser then uses the cookie header in the request to include the cookie that has been created and returns it to the server to complete the browser validation.


Creation of cookies

PHP creates a cookie that needs to call the Setcookie () function, which is structured as follows:

BOOL Setcookie (string name[,string value[,int expiration[,string Path[,bool Secure]])
The name of the parameter indicates the names of the cookies, and other parameters are optional;
The value of the parameter is the values stored in the cookie;
Parameter expiration specifies the time when the cookie expires;
The parameter path specifies a valid path to the cookie on the server that is used to set the cookie to be sent to the server under that folder;
The parameter secure specifies whether the cookie is sent through a secure HTTPS link.
PHP Cookie Instance Code:

The code is as follows Copy Code

Setcookie ("C1", "My name is Rose", Time () +3600);
/* Create a cookie named "C1" expires at 3,600 seconds */
?>

Access to Cookies

Most variables only occupy space in memory, so when the PHP script is terminated, the variables are freed from memory. A cookie can store the value of a variable for a long time on the user's computer's hard disk, and when we need to call the value of the variable, read the name of the cookie.

A cookie named "C1″" has been created in the previous section of the cookie, so let's test if the cookie was created successfully.

Examples of PHP access cookies:

The code is as follows Copy Code

echo "C1 's COOKIE value is". $_cookie[' C1 '). "
";
echo "C2 's COOKIE value is". $_cookie[' C2 ');
?>

The time of the cookie

If the cookie is not set to a valid time, the cookie disappears when the browser is closed. If you want to keep the cookie, you must set a valid time for it.

PHP Cookie Time Setting Method:

Time () + number of seconds
Time () indicates the current user's operating system, and the number of seconds after that is the effective time of the cookie. If you want to set a specific date, you can use the Mktime () function, which is structured as follows:

Mktime (hours, minutes, seconds, months, days, years)
If you want to delete a cookie immediately, you can set the time to a time in the past.

PHP Cookie Time Setting Example:

The code is as follows Copy Code
Setcookie ("A", "Ten", Time ()-60); /* Set time to 60 seconds before its value is immediately deleted */
Setcookie ("A", "Ten", Time () +60); /* Set valid time is 60 seconds */
Setcookie ("A", "Ten", Mktime (0,0,0,11,1,2011)); /* Expiry time is November 1, 2011 0:0 0 seconds */

Here is a simple statistical program using cookie Design:

The code is as follows Copy Code

$count =$_cookie[' user '];
$count + +;
Setcookie ("User", $count, Time () +3600);
echo "This is your first $count visit!";
?>

Cookie Array

We can also create an array of PHP cookies, which are described below with an example of the implementation method.

Code:

The code is as follows Copy Code
Setcookie (' name[1] ', ' Rose ');
Setcookie (' name[2] ', ' John ');
Setcookie (' name[3] ', ' mikle ');
if (Is_array ($_cookie[' name '))) {/* determine if array */
foreach ($_cookie[' name '] as $name = = $value) {
echo "$name: $value
";
}
}
else echo "is not an array";
?>


Restrictions on cookies

Cookies are used to store important information about a user's browsing of the site, and to prevent the user's information from being compromised, there are certain restrictions on cookies:

Browser record the user cookie size limit within 4KB capacity;
The number of cookies that the browser only saves on a site server is limited to 20, and if this amount is exceeded, the previously saved will be deleted.
Each user's browser can access a maximum of 300 cookies.
Users can set whether cookies are stored in the browser settings, so to use cookies to store information, you must first confirm that the function of the cookie in the browser is turned on. For example IE9 Browser, open "Properties" – "Privacy" – "Advanced" option, check enable:


Tip: After a cookie has been created by the browser, each request for the site will be accompanied by this cookie in the header. and the browser will always be sent to know where the cookie expires. However, for requests from other websites, cookies are never sent along with them.

http://www.bkjia.com/PHPjc/628829.html www.bkjia.com true http://www.bkjia.com/PHPjc/628829.html techarticle Cookies are a session that is used in all programs, and below I share my PHP cookie notes with my classmates, this article describes all the tips for getting started with cookies ...

  • 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.