cookie_php Tutorials in PHP

Source: Internet
Author: User
Tags set cookie setcookie
Using PHP to set up and read cookies is extremely--do we dare to say? -Simple things. We don't want to vigorously advocate cookies, but they are both important and practical. They are the only tools that apply when solving certain problems.

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

The simplest way to set cookies is as follows:

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

Then, before the user exits, next to each page in the site viewed by this browser, there will be a variable $name with the value "Bret", and it is easy to access it through PHP. Because the lifetime is a user link, such cookies are referred to as session cookies.

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

But don't be afraid. PHP provides a very useful function mktime (). You just send the order to Mktime () the hour, minute, number of seconds, month, date, and year that you want to represent, and mktime () returns the total number of seconds that date since January 1, 1970. Therefore, if you need to simulate Y2K problems:

$y 2k = mktime (0,0,0,1,1,2000);
Setcookie (' name ', ' Bret ', $y 2k);
?>

Your cookie will now 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 on the previous page, you can still change your name to "Jeff".

$y 2k = mktime (0,0,0,1,1,2000);
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 identify both, you can write the following code:

$name = ' Jeff ';
$y 2k = mktime (0,0,0,1,1,2000);
Setcookie (' name ', $name, $y 2k);
?>

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

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

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

Deleting a cookie is very simple, simply passing the name of the cookie to Setcookie () and PHP will delete it.

Setcookie (' name ');
?>

Finally, there is an important matter of using cookies. Because of the specific way cookies and HTTP work, you must transmit all cookies before you print any text. Otherwise, PHP will give a warning and the cookie will not be transmitted. Therefore, doing so is the right approach:

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

The following are incorrect:

$today = Mktime (12,0,0,6,25,1999);
Echo ' Here it's '. Date (' G:i:s A, F D, Y ', $today);
Echo ';
Echo ' in GMT it's '. Gmdate (' G:i:s A, F D, Y ', $today);
?>

http://www.bkjia.com/PHPjc/317200.html www.bkjia.com true http://www.bkjia.com/PHPjc/317200.html techarticle using PHP to set up and read cookies is extremely--do we dare to say? -Simple things. We don't want to vigorously advocate cookies, but they are both important and practical. They are solutions ...

  • Related Article

    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.