The setcookie function of PHP
definition and usage.
cookie is a variable sent to the browser by the server. Cookie is usually a small text file embedded in the user's computer by the server. Every time a computer requests a page through a browser, it sends the cookie. The name of
cookie is specified as a variable of the same name. For example, if the cookie being sent is called.Quot; name.quot, it will automatically create a variable called $user, which contains the value of cookie.
must assign cookie before any other output is sent. If
is successful, the function returns true, otherwise it returns false.
syntax
setcookie (name, value, expire, path, domain, secure)
parameter description is necessary. Specify the name of cookie.
value is necessary. Specify the value of cookie.
expire is optional. The period of validity of cookie is stipulated.
path is optional. The server path of the cookie is specified.
domain is optional. Specify the domain name of cookie.
secure is optional. Specifies whether cookie can be transmitted through secure HTTPS connections.
prompt and annotate
notes: you can access the value of.Quot; user.quot; user.quot; user.quot; cookie; user.quot; $HTTP_COOKIE_VARS[.quot; user.quot; or $_COOKIE[.quot; user.quot;].
note: when sending cookie, the value of cookie will be automatically encoded by URL. URL decoding is performed when receiving. If you do not need this, you can use setrawcookie () instead.
1
setting and sending cookie:
[code].lt; PHP $value =.Quot; my cookie; Ue.quot; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / > cookie setcookie (.Quot; TestCookie.quot; $value, time () 3600*24);?.gt; [/code]
example 2
.#93; echo.Quot;.Lt; br /.gt;.Quot; echo $HTTP_COOKIE_VARS.#91;.Quot; TestCookie.quot;.#93; R />
example 3
by setting the expiration date to the date / time of the past, delete a cookie:
[code].lt;? PHP / / / / / / / / / / / / / / / / / / / / / / / / / / / / / > to set the expiration date to an hour before setcookie (.Quot; TestCookie.quot;.Quot;) - 3600); An array of cookie:
[code].lt;? PHP setcookie (.Quot; cookie.#91; three.#93;.Quot;.Quot; cookiethree.quot;); (isset ($_COOKIE.#91;.Quot; cookie.quot;.#93;)) {foreach ($_COOKIE.#91;.Quot; cookie.quot;.#93; as $name)