Php creation and retrieval of cookies and Basic Analysis

Source: Internet
Author: User
Tags send cookies
This article mainly introduces the creation and retrieval of cookies in php and the basic points. The example analyzes the php cookie operation skills. For more information, see

This article mainly introduces the creation and retrieval of cookies in php and the basic points. The example analyzes the php cookie operation skills. For more information, see

This example describes how to create, obtain, and analyze cookies in php. Share it with you for your reference. The details are as follows:

Assume that the cookie1.php File

The Code is as follows:

<? Php
SetCookie ("name", "Baidu", time () + 60 );
Echo "Save cookie ";
?>


Three required parameters:

(1) first parameter: name is the key value and is set by yourself;
(2) parameter 2: "Baidu" in the instance indicates the value corresponding to the key value name;
(3) parameter 3: expiration time, time () + 60, indicating that the expiration time is 60 seconds;

Cookie code parsing in the instance and basic points

1. When cookie1.php is opened in the browser, the server displays the following information: Set-Cookie: name = % B0 % D9 % B6 % C8; expires = Tue, 06-Nov-2012 16:09:27 GMT (Remarks: please use the packet capture tool to view this information) to respond to the http request. The client browser obtains this information and saves it to the cookies file (different browsers and operating systems have different storage locations and file types)
2. If the third time parameter is not set, the cookie becomes invalid by default when the session ends (the browser is closed) (in this case, the cookie is saved in the browser cache ).
3. Cookies can only store string information, that is, objects cannot be saved (sessions can save objects ).
4. If the key value is Chinese, urlencode is used by default to transcode Chinese.
5. When the cookie is saved, it is saved in plain text. Therefore, it must be processed when saving the password, such as md5.
6. Multiple cookies can be saved.
7. Different cookies on the same page can be saved at different times.
8. A website has a cookie-saving file (if any ).

Get cookie

File: cookie2.php

The Code is as follows:

<? Php
Echo"

";
Print_r ($ _ COOKIE );
Echo $ _ COOKIE ['name'];
?>


$ _ COOKIE is a pre-defined variable (array). During the cookie validity period, you can use the above Code to output the cookie.
Key knowledge points: when accessing the cookie2.php page, the browser will send cookies to the server, which is stipulated by the http protocol (you can use the packet capture tool to view the information, for example)

I hope this article will help you with php programming.

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.