PHP4 Cookie Support Detailed _php tutorial

Source: Internet
Author: User
Tags setcookie
PHP4 's Cookie support detailed

The establishment of commercial sites or more sophisticated personal sites, often need to record visitors ' information, in PHP provides two convenient means: Session and Cookie function. In order to permanently maintain user information, Cookies are the most convenient means. Here I will give you a detailed explanation of the function of the cookie and how to use it.

One: Set cookies
Cookies must be set before using cookies.
function prototypes: int Setcookie (string name,string value,int expire,string path,string domain,int Secure)
All parameters, except name, are optional and can be represented by an empty string.
Property value: Used to specify a value.
Property path: Used to specify which directory path the cookie is sent to the server.
Attribute domain: Ability to limit the sending of cookies on the browser side.
Expire parameter: Used to specify the effective time of the cookie, which is a standard Unix time stamp.
You can use the time () or the mktime () function to obtain the unit in seconds.
Secure parameter: Indicates whether this cookie is transmitted over the network via an encrypted HTTPS protocol.

Two: Precautions when setting cookies
Setting cookies on the same page is actually done in a backward-forward order. If you want to delete a cookie before writing a cookie, you must write the statement and then write the DELETE statement. Otherwise, an error occurs.

Three: setcookie Example
Simple: Setcookie ("MyCookie", "Value_of_mycookie");
With Expiration: Setcookie ("Withexpire", "Expire_in_1_hour", Time () +3600);
Everything: Setcookie ("Fullcookie", "Full_cookie_value", time+3600, "/forum", "www.123.com", 1);

IV: Some features of cookies
Cookies are path-oriented. When the default Path property is used, the Web server page automatically passes the current path to the browser. Specifying a path forces the server to use the path of the setting.
Cookies placed on one directory page are not visible on the page of another directory.

V: Receive and process cookies
PHP's handling of cookies is fully automated, as is the case with the form variables. Of course, you can also use PHP global variables, $HTTP _cookie_vars arrays.
Example: Echo $mycookie;
echo $cookie array[0];
echo Count ($cookie Array);
echo $HTTP _cookie_vars["MyCookie"];

VI: Delete Cookies
(1) Call Setcookie () with the name parameter only;
(2) to make the expiry time () or time-1;

Seven: Restrictions on the use of cookies
(1) must be set before the content output of the HTML file;
(2) Different browser to the cookie processing inconsistent, must be considered when using;
(3) client-side restrictions, such as user settings prohibit cookies, the cookie can not be established;

Eight: A specific example, I hope you have a more profound understanding of cookies
cookie.php
if (!isset ($flag))
{
Setcookie ("MyCookie", "This My cookie!");
Header ("location:cookie.php?flag=1");
Exit
}
?>


echo "Cookies are:". $mycookie;
?>



How about, through the above introduction, you do not have a detailed understanding of the cookie function in PHP. Use your intelligence to make your site more powerful. Please contact fbigov@sina.com If you have any questions. Let's make progress together.

http://www.bkjia.com/PHPjc/315272.html www.bkjia.com true http://www.bkjia.com/PHPjc/315272.html techarticle PHP4 Cookies Support detailed establishment of commercial sites or more sophisticated personal sites, often need to record visitors ' information, in PHP provides two convenient means: session and cook ...

  • 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.