. What is cookie?
Cookie is a mechanism for storing data on a remote browser to track and identify users. 2. php Tutorial how to set cookie
You can use the setcookie () or setrawcookie () function in php to set the cookie. cookie is a part of the http header. Therefore, setcookie () and other functions must be called before the output data base. This is the same as the header () function. however, you can also use the output buffer function to delay the script output. until all cookies and other http headers are set.
<? Php
$ Value = "something ";
Setcookie ("testcookie", $ value); // Set the cookie value
Setcookie ("testcookie", $ value, time () + 3600); // Set the Expiration time
?>
If you want to set multiple values for a cookie variable, you can add the [] symbol after the cookie variable name.
<? Php
// Set the cookies
Setcookie ("cookie [three]", "cookiethree ");
Setcookie ("cookie [two]", "cookietwo ");
Setcookie ("cookie [one]", "cookieone ");
?>
Setcookie () can also set the domain and directory path of the program that controls the reading of cookies. By default, it can be read only on pages in the same directory structure as the server and within the same level or below, however, if you want to enable/~ from the domain example.com /~ Rasmus directory processing page, and use ssl-based secure connection, you can perform the following settings:
<? Php
$ Value = "something ";
Setcookie ("testcookie", $ value, time () + 3600 ,"/~ Rasmus/"," .example.com ", 1 );
?>
The operation for deleting a cookie is similar to the above. You only need to set the cookie value to null.
<? Php
// Set the expiration date to one hour ago
Setcookie ("testcookie", "", time ()-3600 );
Setcookie ("testcookie", "", time ()-3600 ,"/~ Rasmus/"," .example.com ", 1 );
?> Note:
According to register_globals settings, common php variables can be created from cookies. However, this feature is not recommended because this option is usually disabled for security reasons.
Make sure that no data is output before setting the cookie.
Php automatically receives and processes cookies. You only need to call the cookie directly when you need it. You can use print_r ($ _ cookie) to print out all the values of the cookie array.