Php setcookie introduction and instance application

Source: Internet
Author: User
Tags set cookie setcookie

The cookie name is a variable with the same name. For example, if the sent cookie is named "name", a variable named $ user is automatically created, containing the cookie value.

The cookie must be assigned a value before any other output is sent.

If the call succeeds, the function returns true; otherwise, the function returns false.

Syntax
Setcookie (name, value, expire, path, domain, secure)

*/
$ Cookieinfo = session_get_cookie_params (); // Obtain cookie information
If (empty ($ cookieinfo ['domain ']) & (empty ($ cookieinfo ['Secure']) // check whether the result is null
{
Setcookie (session_name (), '', time ()-3600, $ cookieinfo ['path']); // Set cookie
}
Elseif (empty ($ cookieinfo ['Secure ']) // check whether the option is null
{
Setcookie (session_name (), '', time ()-3600, $ cookieinfo ['path'], $ cookieinfo ['domain']); // Set cookie
}
Else
{
Setcookie (session_name (), '', time ()-3600, $ cookieinfo ['path'], $ cookieinfo ['domain'], $ cookieinfo ['Secure ']); // Set cookie
}
// Session_destroy (); // cancel the session
Print_r ($ _ session );
Print_r ($ _ cookie );

// You can use session_set_cookie_params to set the lifetime and path.

Session_set_cookie_params (0, '/yourpath/'); // Set the cookie survival time and path


/*

Note: You can use $ http_cookie_vars ["user"] or $ _ cookie ["user"] to access the cookie value named "user.

Note: When sending a cookie, the cookie value is automatically url encoded. Url decoding is performed when receiving the message. If you do not need this, you can use setrawcookie () instead.

*/

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.