Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the setcookie parameter in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what is the use of setcookie parameters in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Setcookie () defines a cookie that is sent with the rest of the HTTP headers. Like other headers, cookie must be sent before any other output of the script (this is a protocol limitation). This requires putting the call to this function before any output, including the and tags and any spaces. If there is any output before calling setcookie (), this function will fail and return FALSE. If the setcookie () function runs successfully, it returns TRUE. This does not indicate whether the user has accepted cookie.

Function definition:

Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])

Detailed explanation of setcookie () parameters

The parameter description shows that the name of the example namecookie uses $_ COOKIE ['cookiename'] to call the cookie named cookiename. The value of valuecookie, stored on the client side, do not store sensitive data assuming that name is' cookiename', can be obtained through $_ COOKIE ['cookiename']. Expire

The time when the Cookie expired. This is a Unix timestamp, that is, the number of seconds since the Unix era.

In other words, the expiration period of cookie is usually set by the time () function plus the number of seconds.

Or use mktime () to implement it.

Time () + 60,60,24,30 will set cookie to expire after 30 days.

If it is not set, cookie will expire at the end of the session (usually when the browser is closed).

The valid path of the pathCookie on the server side.

If this parameter is set to'/', the cookie is valid throughout the domain

If set to'/ foo/',cookie, it is only valid within the / foo/ directory and its subdirectories under domain, for example, / foo/bar/.

The default value is to set the current directory of cookie.

Domain the cookie valid domain name.

For cookie to be valid in all subdomains such as the example.com domain name, this parameter should be set to '.example.com'.

Although. It's not necessary, but plus it will be compatible with more browsers.

If this parameter is set to www.example.com, it is valid only in the www subdomain.

For details, see tail matching in the Cookie specification.

Secure

Indicates whether the cookie is sent only over a secure HTTPS connection.

When set to TRUE, cookie is set only in secure connections. The default is FALSE.

0 or 1

Example 1. Setcookie () sending example

The copy code is as follows:

$value = 'something from somewhere'

Setcookie ("TestCookie", $value)

Setcookie ("TestCookie", $value,time () + 3600); / * expire in 1 hour * /

Setcookie ("TestCookie", $value,time () + 3600, "/ ~ rasmus/", ".utoronto.ca", 1)

Note that the part of the value in cookie is automatically encoded in urlencode when sent and automatically decoded when received and assigned to the cookie variable with the same name. If you don't want to and are using PHP 5, you can use setrawcookie () instead. Here is a simple example to get the value of cookie you just set:

The copy code is as follows:

To delete cookie, you need to make sure that its expiration date is in the past before you can trigger the browser's deletion mechanism. The following example shows how to delete the cookie you just set:

Example 2. Setcookie () delete example

The copy code is as follows:

/ / set the expiration time to one hour ago

Setcookie ("TestCookie", ", time ()-3600)

Setcookie ("TestCookie", "", time ()-3600, "/ ~ rasmus/", ".utoronto.ca", 1)

You can also set the array cookie by using array symbols in the cookie name. You can set multiple cookie as array units, and when the script extracts the cookie, all the values are placed in one array:

Example 3. Example of using an array in setcookie ()

The copy code is as follows:

The above example outputs:

Three: cookiethree

Two: cookietwo

One: cookieone

The above is all the content of the article "what is the use of setcookie parameters in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report