Home >Backend Development >PHP Tutorial >php learning cookies

php learning cookies

黄舟
黄舟Original
2016-12-24 09:39:441376browse

Cookies are frequently used knowledge in development. Let me tell you about the common sense and usage of cookies in PHP learning.
What are cookies?
According to the definition in the official documentation, cookies are a way for a server or script to maintain information on the client's workstation under the http protocol. A cookie is a small text file saved on the user's browser by a web service, which contains information about the visiting user. It is a different solution than session, and also solves the problem of maintaining state between multiple things, while maintaining a neat URL; it can be saved on the client machine in the form of a script, and can be saved by sending a message containing specific data and Set a cookie on the user's machine with an http header in the following format:
Set-Cookie:Cookie_Name=MyFirstCookie;[expires=DATE;][path=PATH]
[domain=DAMAIN_NAME;][secure]
The above code will create A cookie named Cookie Name with the value MyFirstCookie. Parameters within square brackets [] are optional. The parameter expires field sets the cookie expiration date. If it is not set and is not manually deleted, the cookie will be valid forever; path and domain are combined to specify the URL or cookie-related URL; session The keyword means that cookies are not sent in ordinary http connections.
How to use cookies in php?
To use cookies in php, you must set cookies first. We can use SetCookie() to set cookies manually.
For example: SetCookie(string name [,string value [,int expire [,string path [,string domain [,int secure]]]]]);
Except name, everything else is optional.
Simple example of setting cookies:
SetCookie('NAME','messi');
SetCookie('NAME', 'messi', time()+3600,'/web');

The above is the cookie for php learning For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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
Previous article:PHP learning sessionNext article:PHP learning session