PHP Setcookie () usage, Phpsetcookie usage _php tutorial

Source: Internet
Author: User

PHP Setcookie () usage, Phpsetcookie usage


Definition and usage

The Setcookie () function sends an HTTP cookie to the client.

A cookie is a variable that is sent to the browser by the server. A cookie is usually a small text file that the server embeds into a user's computer. This cookie is sent whenever the computer requests a page through a browser.

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

There can be no loss before a cookie is assigned a value. If successful, the function returns True, otherwise false is returned.

Note:cookie settings must be refreshed later to take effect.

Grammar

    1. Setcookie(name,value,expire,path,domain,secure)
Parameters Description
Name Necessary. Specifies the name of the cookie.
Value Necessary. Specifies the value of the cookie.
Expire Optional. Specify the validity period of the cookie.
Path Optional. Specifies the server path of the cookie.
Domain Optional. Specifies the domain name of the cookie.
Secure Optional. Specifies whether to transfer cookies through a secure HTTPS connection.

Hints and Notes

Note: You can access the value of a COOKIE named "User" by $HTTP _cookie_vars["user"] or $_cookie["user".

Note: When a cookie is sent, the value of the cookie is automatically URL-encoded. The URL is decoded when it is received. If you don't need this, you can use Setrawcookie () instead.

Example 1

Set up and send cookies:

    1. !--? PHP
    2. $value = My cookie value "
    3. //send a simple cookie
    4. setcookie ( "TestCookie" $value
    5. ?> ...
    1. Php
    2. $value = "My cookie Value";
    3. Send a cookie that expires in the 24 child
    4. Setcookie("TestCookie",$value, time() +3600*);
    5. ?> ...

Example 2

Different ways to retrieve cookie values:

    1. !--? PHP
    2. //output individual cookies
    3. echo $_cookie[ "TestCookie" ;
    4. echo "
      "
    5. echo $HTTP _cookie_vars[ "TestCookie"
    6. echo "
      " ;
    7. //output all Cookies
    8. print_r ($_cookie
    9. ?>

Output:

    1. My cookie value
    2. My cookie value
    3. Array ([testcookie] = my cookie value)

Example 3

Delete a cookie by setting the expiration date to the last Date/time:

    1. Php
    2. Set the expiration date to one hour ago
    3. Setcookie ("TestCookie", " ", time() - 3600);
    4. ?> ...

Example 4

Create an array cookie:

  1. Php
  2. Setcookie("Cookie[three]", "cookiethree");
  3. Setcookie("cookie[two]", "cookietwo");
  4. Setcookie("Cookie[one]", "cookieone");
  5. Output cookie (after reloading the page)
  6. If (isset($_cookie["COOKIE"])) {
  7. foreach ($_cookie["COOKIE"] as $name + = $value) {
  8. Echo "$name: $value
    ";
  9. }
  10. }
  11. ?> ...

Output:

    1. Three : cookiethree
    2. Both : cookietwo
    3. One : cookieone

Example 5

An issue that does not take effect when a cookie is set. Usually the reason why the scope is not set

    1. Php
    2. Setcookie("A","BB", Time() +3600,"/",". hi-docs.com");
    3. Note that the domain name is set to its own
    4. ?>

http://www.bkjia.com/PHPjc/1048751.html www.bkjia.com true http://www.bkjia.com/PHPjc/1048751.html techarticle PHP Setcookie () usage, Phpsetcookie usage definitions and usage setcookie () function sends an HTTP cookie to the client. A cookie is a variable that is sent to the browser by the server. Cookies are usually ...

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