The cookie_php techniques in PHP

Source: Internet
Author: User
Tags set cookie setcookie
Using PHP to set and read cookies is an extremely--we dare say? -The simple thing. We don't want to preach cookies, but they are both important and practical. They are the only tools available to solve certain problems.

To create and modify a cookie, you can use the PHP function Setcookie (). Depending on the degree to which you want to control the cookie, and who can read the value of the cookie, Setcookie () can have up to six parameters.

The easiest way to set cookies is as follows:

<?php
Setcookie(' name ', ' Bret ');
?>

Then, before the user exits, every page in the site that is viewed using this browser will have a variable $name with a value of "Bret", and it is easy to access it through PHP. This type of cookie is called the session cookie because its lifetime is a user link.

If you want the user to close their browser and still retain this cookie, you must pass the third argument to the Setcookie () function, which is the valid date for this cookie. Since PHP's background is entirely rooted in Unix thinking, the expiration date is represented by the total number of seconds starting from January 1, 1970. If you are a UNIX programmer, this algorithm may be reasonable for you. But if you're from Windows or the Macintosh camp, you may just have to shake your head and not understand the weird Unix guys.

But don't be afraid. PHP provides a very useful function mktime (). You just send it to mktime () The hours, minutes, seconds, months, dates, and years that you want to represent, mktime () returns the total number of seconds since January 1, 1970. Therefore, if you need to simulate a Y2K problem:

<?php
$y 2k = mktime(0,0,0, 1 , 1 , Watts );
setcookie(' name ', ' Bret ', $y 2k );
?>

Now, your cookie will expire in 2000.

If you need to update the cookie so that it stores the new value, simply overwrite its original value. So, even if you've just sent a cookie to the previous page, you can still change your name to "Jeff."

<?php
$y 2k = mktime(0,0,0, 1 , 1 , Watts );
setcookie(' name ', ' Jeff ', $y 2k );
?>

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

<?php
$name = ' Jeff ' ;
$y 2k = mktime(0,0,0 ,1,1,Watts );
setcookie(' name ', $name, $y 2k );
?>

The next two parameters of Setcookie () can control the domain and directory path of the program that reads cookies. The default is set to read only on pages that are identical to the server that sent the cookie and that are within the directory structure of the same sibling or below. This is due to network security considerations. However, if you have an account "www.domain.com" but are also "other.domain.com" and the account allows the page to be processed from the ~/myhome directory, you should change the Setcookie () as follows:

<?php
Setcookie(' name ', ' Jeff ', $y 2k, ' ~/ MyHome ', '. domain.com ');
?>

The last parameter that we have not used Setcookie () is that the set cookie is sent only to 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, simply passing the name of the cookie to Setcookie () and PHP will remove it.

<?php
Setcookie(' name ');
?>

Finally, there is an important issue about using cookies. Because cookies and HTTP work in a specific way, you must send out all the cookies before you output any text. Otherwise, PHP will give a warning and the cookie will not be sent. So this is the right way to do this:

<?php
Setcookie(' name ', ' Jeff ');
echo "Hello everyone!" ;
?>

The following is erroneously:

<?php
$today = mktime(0,0, 6 , - , 1999 );
echo ' Here it is '. Date (' g:i:s A, F D, Y ',$today );
echo "" ;
echo ' in GMT it is '. gmdate (' g:i:s A, F D, Y ',$today );
?>

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.