PHP - setcookie
Trieda
Metóda - setcookie
(PHP 4, PHP 5, PHP 7)
The function defines and sets a cookie.
Note: The function has to be called before any output (both
caused by the echo() function or just by sending a HTML fragment
before the PHP directive). Typically, the function is called in the PHP block on
the very first lines of the index.php file. This limitation is caused by the
fact cookies are sent in the HTTP header. Once we produce any output, the HTTP
header is sent and cannot be sent again.
Note: The cookie is automatically URL-encoded (when is sent)
and automatically URL-decoded (when is received). If we don't want it, we can
use the setrawcookie() function.
Procedurálne
- function setcookie (string $name, string $value = , int $expire = 0, string $path = , string $domain = , bool $secure = false, bool $httponly = false) : bool
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $name | string | Specifies the name of the cookie. | |
| $value | string | Specifies the value of the cookie. Since the cookie is stored in the client's computer, don't store sensitive information there. | |
| $expire | int | 0 | Specifies the time when the cookie will expire. The time is set in seconds
(as the Unix timestamp, the number of seconds since 1/1/1970) and mostly is set
as |
| $path | string | Specifies the path (on the server) for which the cookie will be available. Examples:
| |
| $domain | string | Specifies the domain for which the cookie is available. We can make it available for the entire domain (and its subdomains) by setting the parameter to e.g. "mypage.com". If it is set to a value like "blog.mypage.com", the cookie is available only for the given subdomain (and all its subdomains). | |
| $secure | bool | false | If set to |
| $httponly | bool | false | In case it's set to |
Mávratovej hodnoty
Vracia: bool
If the function runs successfully, it returns true. Returns
false in case an ouput has been already sent. The value is not
influenced by the fact whether the user has accepted the cookie or not.
Príklady
We set a secured cookie with the name "user_name" which will expire in 30 minutes.
<?php
setcookie("user_name", "Chuck Norris", time() + 30 * 60, "", "", false, true);
We can check that the cookie is set:
<?php
echo (isset($_COOKIE["user_name"])) ? "Value of the cookie is: " . $_COOKIE["user_name"] . "<br>" : "Cookie user_name is not set! <br>";
If we want to modify the cookie, we set the cookie again with the modified information.
<?php
// We change value of the cookie to "John Rambo" and its expiration to 1 hour.
setcookie("user_name", "John Rambo", time() + 60 * 60, "", "", false, true);
We can check the cookie again:
<?php
echo (isset($_COOKIE["user_name"])) ? "Value of the cookie is: " . $_COOKIE["user_name"] . "<br>" : "Cookie user_name is not set! <br>";
If we want to delete the cookie, we set the expiration time in the past.
<?php
setcookie("user_name", "", time() - 1000, "", "", false, true);
We can check the cookie again:
<?php
echo (isset($_COOKIE["user_name"])) ? "Value of the cookie is: " . $_COOKIE["user_name"] . "<br>" : "Cookie user_name is not set! <br>";
If we want to check whether cookies are allowed (enabled) by the user, we can use a small script:
<?php setcookie("test_cookie_allowed", "test", time() + 60); ?> <html> <body> <?php // We are checking, whether the $_COOKIE contains any element. echo (count($_COOKIE) > 0) ? "Cookies are enabled." : "Cookies are disabled."; ?> </body> </html>
Súvisiace manuály
- function header () : void
