PHP -zelfstudie

PHP HOME PHP-intro PHP-installatie PHP-syntaxis PHP-opmerkingen PHP-variabelen PHP-echo / afdrukken PHP-gegevenstypen PHP-strings PHP-nummers PHP-wiskunde PHP-constanten PHP-operators PHP Als...Anders...Anders PHP-switch PHP-loops PHP-functies PHP-arrays PHP Superglobals PHP RegEx

PHP- formulieren

PHP-formulierverwerking PHP-formuliervalidatie PHP-formulier vereist URL/e-mail van PHP-formulier PHP-formulier voltooid

PHP Geavanceerd

PHP-datum en tijd PHP opnemen PHP-bestandsverwerking PHP-bestand openen/lezen PHP-bestand maken/schrijven PHP-bestand uploaden PHP-cookies PHP-sessies PHP-filters PHP-filters geavanceerd PHP-callback-functies PHP JSON PHP-uitzonderingen

PHP OOP

PHP Wat is OOP PHP-klassen/objecten PHP-constructor PHP-vernietiger PHP-toegangsmodificaties PHP-overerving PHP-constanten PHP abstracte lessen PHP-interfaces PHP-kenmerken PHP statische methoden Statische eigenschappen van PHP PHP-naamruimten PHP-iterables

MySQL- database

MySQL-database MySQL Connect MySQL DB maken MySQL-tabel maken MySQL Gegevens invoegen MySQL Laatste ID ophalen MySQL Meerdere invoegen MySQL voorbereid MySQL Gegevens selecteren MySQL Waar MySQL Bestel op MySQL Gegevens verwijderen MySQL-updategegevens MySQL-limietgegevens

PHP XML

PHP XML-parsers PHP SimpleXML-parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

Ajax-intro AJAX PHP AJAX-database AJAX XML Live zoeken in Ajax Ajax-peiling

PHP- voorbeelden

PHP-voorbeelden PHP-compiler PHP-quiz PHP-oefeningen PHP-certificaat

PHP- referentie

PHP-overzicht PHP-array PHP-agenda PHP-datum PHP-map PHP-fout PHP-uitzondering PHP-bestandssysteem PHP-filter PHP FTP PHP JSON PHP-sleutelwoorden PHP Libxml PHP-e-mail PHP-wiskunde PHP Diversen PHP MySQLi PHP-netwerk PHP-uitvoercontrole PHP RegEx PHP SimpleXML PHP-stream PHP-string Beheer van PHP-variabelen PHP XML-parser PHP-zip PHP-tijdzones

PHP setcookie() Functie

❮ PHP-netwerkreferentie

Voorbeeld

In het volgende voorbeeld wordt een cookie gemaakt met de naam "user" met de waarde "John Doe". De cookie verloopt na 30 dagen (86400 * 30). De "/" betekent dat de cookie op de hele website beschikbaar is (anders selecteert u de map die u verkiest).

We halen dan de waarde van de cookie "user" op (met behulp van de globale variabele $_COOKIE). We gebruiken ook de isset()-functie om erachter te komen of de cookie is ingesteld:

<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Definitie en gebruik

De functie setcookie() definieert een cookie die samen met de rest van de HTTP-headers moet worden verzonden.

Een cookie wordt vaak gebruikt om een ​​gebruiker te identificeren. Een cookie is een klein bestand dat de server op de computer van de gebruiker insluit. Elke keer dat dezelfde computer een pagina met een browser opvraagt, verzendt deze ook de cookie. Met PHP kunt u zowel cookie-waarden maken als ophalen.

De naam van de cookie wordt automatisch toegewezen aan een variabele met dezelfde naam. Als er bijvoorbeeld een cookie is verzonden met de naam "user", wordt automatisch een variabele gemaakt met de naam $user, die de cookiewaarde bevat.

Opmerking: de functie setcookie() moet vóór de tag <html> verschijnen.

Opmerking: de waarde van de cookie wordt automatisch URL-gecodeerd bij het verzenden van de cookie, en automatisch gedecodeerd wanneer deze wordt ontvangen (gebruik in plaats daarvan setrawcookie() om URL-codering te voorkomen ).

Syntaxis

setcookie(name, value, expire, path, domain, secure, httponly);

Parameterwaarden

Parameter Description
name Required. Specifies the name of the cookie
value Optional. Specifies the value of the cookie
expire Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Default is 0
path Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in
domain Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to "example.com". Setting it to www.example.com will make the cookie only available in the www subdomain
secure Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE
httponly Optional. If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE


Technische details

Winstwaarde: WAAR op succes. ONWAAR bij fout
PHP-versie: 4+
PHP-wijzigingslogboek: PHP 5.5 - Een Max-Age-attribuut is opgenomen in de Set-Cookie-header die naar de client is verzonden
PHP 5.2 - De parameter httponly is toegevoegd

Meer voorbeelden

Voorbeeld

Verschillende vervaldata voor cookies:

<?php
$value = "Hello world!";

// cookie will expire when the browser close
setcookie("myCookie", $value);

// cookie will expire in 1 hour
setcookie("myCookie", $value, time() + 3600);

// cookie will expire in 1 hour, and will only be available
// within the php directory + all sub-directories of php
setcookie("myCookie", $value, time() + 3600, "/php/");
?>
<html>
<body>

...some code...

</body>
</html>

Voorbeeld

Om een ​​cookie te wijzigen, plaatst u de cookie (opnieuw) met behulp van de setcookie()-functie:

<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

</body>
</html>

Voorbeeld

Gebruik de functie setcookie() met een vervaldatum in het verleden om een ​​cookie te verwijderen:

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>

Voorbeeld

Maak een klein script dat controleert of cookies zijn ingeschakeld. Probeer eerst een testcookie te maken met de functie setcookie() en tel vervolgens de arrayvariabele $_COOKIE:

<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
    echo "Cookies are enabled.";
} else {
    echo "Cookies are disabled.";
}
?>

</body>
</html>

❮ PHP-netwerkreferentie