NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - setcookie

Trieda

Koreň \ Bez triedy

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ázovDátový typPredvolená hodnotaPopis
$namestring

Specifies the name of the cookie.

$valuestring

Specifies the value of the cookie. Since the cookie is stored in the client's computer, don't store sensitive information there.

$expireint 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 time() + 7*60*60 (the current time + 7 hours expressed in seconds). If not specified, the cookie will expire when the browser will be closed (at the end of the session).

$pathstring

Specifies the path (on the server) for which the cookie will be available. Examples:

  • "/" = the cookie is available within the entire domain.
  • "/cookies/" = the cookie is available within the entire directory (in this case: "cookies").
  • The default value = the current directory the cookie is being set.
$domainstring

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).

$securebool false

If set to true, the cookie will be transmitted only over a secured HTTPS connection.

$httponlybool false

In case it's set to true, the cookie is accessible only through the HTTP protocol (not by scripting languages like JavaScript). This setting highly reduces the risk of stealing the user relation. The default value is unfortunately false.

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

      Aktivity