Detailed explanation of COOKIE support in PHP4_PHP tutorial

WBOY
Release: 2016-07-21 16:06:53
Original
764 people have browsed it

Detailed explanation of COOKIE support in PHP4

When establishing a commercial website or a personal website with relatively complete functions, it is often necessary to record visitor information. PHP provides two convenient means: session and cookie functions. In order to maintain it permanently User information, cookies are the most convenient way. Here I will explain the functions and usage of cookies in detail.

1: Set cookies
Cookies must be set before using cookies.
Function prototype: int setcookie(string name, string value, int expire, string path, string domain, int secure)
Among them, except name, all parameters are optional, and an empty string can be used to indicate Settings.
Attribute value: used to specify the value.
Attribute path: used to specify the directory path where the cookie is sent to the server.
Attribute domain: can be used to send cookies on the browser side Limited.
expire parameter: used to specify the validity time of the cookie, which is a standard Unix time stamp.
It can be obtained using the time() or mktime() function, in seconds.
secure parameter : Indicates whether this cookie is transmitted over the network through the encrypted HTTPS protocol.

2: Things to note when setting cookies
Setting cookies on the same page is actually in order from back to front Performed. If you want to delete a cookie first and then write a cookie, you must write the write statement first and then the delete statement. Otherwise, an error will occur.

Three: setcookie example
Simple: setcookie("mycookie","value_of_mycookie");
With expiration time: setcookie("withExpire","Expire_in_1_hour",time()+3600);
It has everything: setcookie("FullCookie", "Full_cookie_value",time+3600,"/forum","www.123.com",1);

Four: Some characteristics of cookies
Cookies are path-oriented. The default path attribute is , the WEB server page will automatically pass the current path to the browser. Specifying the path will force the server to use the set path.
Cookies set in one directory page cannot be seen in another directory page.

Five: Receiving and processing cookies
PHP’s processing of cookies is fully automatic, and the principle of processing FORM variables is the same. Of course, you can also use PHP global variables, the $HTTP_COOKIE_VARS array.
Example: echo $mycookie ;
echo $cookie Array[0];
echo count($cookie Array);
echo $HTTP_COOKIE_VARS["mycookie"];

Six: Delete cookies
 (1 ) Call setcookie() with only the name parameter;
(2) Make the expiration time time() or time-1;

7: Restrictions on using cookies
(1) Must be in The content of the HTML file is set before output;
(2) Different browsers handle cookies inconsistently, so you must consider them when using them;
(3) Client restrictions, such as user settings prohibiting cookies, the cookie cannot be created ;

Eight: A specific example, I hope everyone will have a deeper understanding of cookies
🎜>                                                                                                                                }
                                                                                                                                                                ;/Body & gt;
& lt;/html & gt;

How about, do you have a detailed understanding of the cookie function in PHP through the above introduction. Give it to your wisdom, let you make you your ingenuity, let you make you Let’s make the site more powerful. If you have any questions, please contact fbigov@sina.com. Let’s make progress together.



http://www.bkjia.com/PHPjc/315272.html

www.bkjia.com

true

TechArticleDetailed explanation of COOKIE support for PHP4 When establishing a commercial site or a personal site with relatively complete functions, it is often necessary to record visitor information. Two convenient methods are provided in PHP: session and cook...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!