PHP Create, get cookies and basic essentials analysis _php Tips

Source: Internet
Author: User
Tags set cookie browser cache

This article analyzes the PHP creation, access cookies, and basic essentials. Share to everyone for your reference. Specifically as follows:

Suppose to be: cookie1.php file

Copy Code code as follows:
<?php
Setcookie ("name", "Baidu", Time () +60);
echo "Save Cookie";
?>

The required parameters are three:

(1) The first parameter: name is the key value, set yourself;
(2) The second parameter: the "Baidu" in the example indicates the value of the key value name;
(3) The third parameter: the Expiration Time () +60, which indicates that the expiration time is 60 seconds;

The cookie code parsing in the instance and the basic points

1, the browser to open cookie1.php, the server will be information: SET-COOKIE:NAME=%B0%D9%B6%C8; Expires=tue, 06-nov-2012 16:09:27 GMT (Note: This information is viewed using the grab kit) to respond to HTTP requests, which the client browser obtains and saves to the cookie file (different browsers and operating systems, Save location is not the same as file type
2. If the third time parameter is not set, the default is the end of the session (the browser is closed), and the cookie is invalidated (in this case, the cookie is saved in the browser cache).
3, cookies can only save string information, that is, objects can not be saved (session to save objects).
4, if the key value is Chinese, by default, the UrlEncode way, the Chinese to transfer code.
5, the cookie is saved, the clear way to save, so, save the password need to deal with, such as MD5.
6, can save multiple cookies.
7, the same page of different cookies, save time can be set differently.
8, a Web site should have a file to save cookies (if there is a set cookie).

Get cookies

File: cookie2.php

Copy Code code as follows:
<?php
echo "<pre>";
Print_r ($_cookie);
echo $_cookie[' name '];
?>

$_cookie is a predefined variable (array) that can be used to output cookies within the valid time of the cookie.
Knowledge Points: When visiting the cookie2.php page, the browser will send the cookies information to the server, which is stipulated by the HTTP protocol (can be viewed by grasping the package tool, as shown below)

I hope this article will help you with your PHP program design.

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.