Cookie in PHP

zhaozj2021-02-16  111

Cookie2004-05-22 Builder.com in PHP print self: West Stocks: http://www.xjtusky.com/Article/Article.php/375

Cookie in PHP

Use PHP to set and read cookies is an extreme - we dare to say? - Simple things. We don't want to advocate cookies, but they are both important and practical. They are the only applicable tools when solving certain problems.

To create and modify a cookie, you can use the PHP function setCookie (). Depending on the level of controlling the cookie, and who can read the value of the cookie, setcookie () can have up to six parameters.

To set up the simplest way as follows:

SetCookie ('Name', 'Bret');

Then, before the user exits, you will have a value of "Bret" with a value of "Bret" with a value of "Bret", and it is easy to access it via PHP. Since its survival is a user link, such cookies are called session cookies.

If you want the user to close its browser, you still keep this cookie, you must pass the third parameter to the SetCookie () function, that is, the valid date of this cookie. Since PHP's background is completely from UNIX, this payment needs to be represented by total seconds from January 1, 1970. If this algorithm may be reasonable for you as a UNIX programmer. But if you can only shake your head and sigh, you can't understand those weird UNIX guys.

But there is no need to be afraid. PHP provides a good function mktime (). You only need to deliver the hour, minute, second, month, date, and year, Mktime () you want to represent in order, MkTime () will return to the total number of seconds from January 1, 1970. Therefore, if you need to simulate Y2K questions:

$ y2k = mktime (0, 0, 0, 1, 1, 2000);

SetCookie ('Name', 'Bret', $ Y2K);

?>

Your cookie will now be invalidated in 2000.

If you need to update your cookie to save your new value, you only need to overwrite its original value. Therefore, even if you have just sent cookies in the previous page, you can still change your name to "Jeff".

$ y2k = mktime (0, 0, 0, 1, 1, 2000);

SetCookie ('Name', 'Jeff', $ Y2K);

?>

Note that this does not change the value of the variable $ NAME. When the page is loaded, its value has been determined. If you want to always determine both, you can write the following code:

$ Name = 'Jeff';

$ y2k = mktime (0, 0, 0, 1, 1, 2000);

SetCookie ('Name', $ Name, $ Y2K);

?>

The next two parameters of SetCookie () can control the domains and directory paths of the program that read the cookie. The default setting is only available only if the server sent by the servers sent out of the cookie and within the same level or the following directory structure can be read. This is for network security considerations. However, if you have an account "www.domain.com" but it is also "other.domain.com", and the account allows the setcookie () as follows:

SetCookie ('Name', 'Jeff', $ Y2K, '~ / MyHome', '.domain.com');?>

The last parameter we have not used is the setting of the Cookie only transmits only a Web server that implements a secure connection such as SSL. To use this feature, set the sixth value to 1.

Deleting cookies is very simple, just simply passing the cookie's name to setCookie (), PHP will delete it.

Finally there is an important thing about using cookie. Due to the specific way of work with HTTP, you must pass all cookies before you output any text. Otherwise, PHP will give a warning and cookie will not be transmitted. Therefore, this is the correct way:

SetCookie ('Name', 'Jeff');

Echo "Hello Everyone!";

?>

The following is incorrect:

Echo "Hello Everyone!";

SetCookie ('Name', 'Jeff');

?>

David Sklar is the CEO of Student.Net Publishing.

ADAM TRACHTENBERG is a deputy authority of Student.Net Publishing product.

转载请注明原文地址:https://www.9cbs.com/read-10452.html

New Post(0)