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

Example of PHP method to set setcookie

Release: 2023-04-08 13:46:01
forward
3554 people have browsed it

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)
Copy after login

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>......
Copy after login
<?php
    $value = "my cookie value";
    // 发送一个 24 小时候过期的 cookie
    setcookie("TestCookie",$value, time()+3600*24);
    ?><html><body>......
Copy after login

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>
Copy after login

Output:

my cookie value
my cookie value
Array ([TestCookie] => my cookie value)
Copy after login

Example 3

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

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

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>......
Copy after login

Output:

three : cookiethree
    two : cookietwo
    one : cookieone
Copy after login

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");
    // 注意把域名设置为自己的
    ?>
Copy after login

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!

Related labels:
php
source:oschina.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template