Home >Backend Development >PHP Tutorial >Example of PHP method to set setcookie

Example of PHP method to set setcookie

尚
forward
2020-03-28 09:14:273557browse

Example of PHP method to set setcookie

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

Cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the computer requests a page through the 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 cookie's value.

There cannot be any input before assigning a value to the cookie. The function returns true if successful, false otherwise.

Note: The cookie setting must be refreshed before it can take effect.

Syntax

setcookie(name,value,expire,path,domain,secure)

Parameter Description

name Required. Specifies the name of the cookie.

value Required. Specifies the cookie value.

expire Optional. Specifies the validity period of the cookie.

path Optional. Specifies the server path for cookies.

domain Optional. Specifies the domain name for the cookie.

secure Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.

Tips and Notes

Note: The cookie named "user" can be accessed through $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"] value.

Note: When sending a cookie, the cookie value is automatically URL encoded. URL decoding occurs on reception. If you don't need this, you can use setrawcookie() instead.

Example 1

Set and send a cookie:

  <?php
    $value = "my cookie value";
    // 发送一个简单的 cookie
    setcookie("TestCookie",$value);
    ?><html><body>......
<?php
    $value = "my cookie value";
    // 发送一个 24 小时候过期的 cookie
    setcookie("TestCookie",$value, time()+3600*24);
    ?><html><body>......

Example 2

Different ways to retrieve cookie values:

<html><body><?php
    // 输出个别的 cookie
    echo $_COOKIE["TestCookie"];
    echo "<br />";
    echo $HTTP_COOKIE_VARS["TestCookie"];
    echo "<br />";
    // 输出所有 cookie
    print_r($_COOKIE);
    ?></body></html>

Output:

my cookie value
my cookie value
Array ([TestCookie] => my cookie value)

Example 3

Delete a cookie by setting the expiration date to a date/time in the past:

<?php
    // 把失效日期设置为一小时前
    setcookie ("TestCookie", "", time() - 3600);
    ?><html><body>......

Example 4

Create An array cookie:

<?php
    setcookie("cookie[three]","cookiethree");
    setcookie("cookie[two]","cookietwo");
    setcookie("cookie[one]","cookieone");
    // 输出 cookie (在重载页面后)
    if (isset($_COOKIE["cookie"])){
      foreach ($_COOKIE["cookie"] as $name => $value){
        echo "$name : $value <br />";    
      }  
    }
    ?><html><body>......

Output:

three : cookiethree
    two : cookietwo
    one : cookieone

Example 5

About the problem that the cookie does not take effect after being set. Usually the reason is that the scope is not set

<?php
    setcookie("a","bb",time()+3600,"/",".hi-docs.com");
    // 注意把域名设置为自己的
    ?>

Recommendation:PHP video tutorial

The above is the detailed content of Example of PHP method to set setcookie. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete