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 setrawcookie() Functie

❮ PHP-netwerkreferentie

Voorbeeld

In het volgende voorbeeld wordt een cookie gemaakt met PHP. De cookie heet "gebruiker" en de waarde is "John Doe". De cookiewaarde is niet URL-gecodeerd. De cookie verloopt na 30 dagen (86400 * 30). Het gebruik van "/" betekent dat de cookie op de hele website beschikbaar is (anders selecteert u de map die u verkiest):

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

<?php
echo "Cookie is set.";
?>

</body>
</html>
?>

Definitie en gebruik

De functie setrawcookie() definieert een cookie (zonder URL-codering) 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 setrawcookie() moet VOORDAT de tag <html> verschijnt.

Opmerking: om de cookiewaarde automatisch te URL-coderen bij het verzenden en automatisch te decoderen bij ontvangst, gebruikt u in plaats daarvan de functie setcookie() .

Syntaxis

setrawcookie(name, value, expire, path, domain, secure);

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 not set, the cookie will expire at the end of the session (when the browser closes)
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.


Technische details

Winstwaarde: WAAR op succes. ONWAAR bij fout
PHP-versie: 5+

Meer voorbeelden

Voorbeeld

Haal de waarde op van de cookie genaamd "user" (met behulp van de globale variabele $_COOKIE). Gebruik ook de isset()-functie om te zien of de cookie bestaat:

<html>
<body>

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

</body>
</html>

Voorbeeld

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

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

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

</body>
</html>

Voorbeeld

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

<?php
$cookie_name = "user";
unset($_COOKIE[$cookie_name]);
// empty value and expiration one hour before
$res = setrawcookie($cookie_name, '', 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 setrawcookie() en tel vervolgens de arrayvariabele $_COOKIE:

<?php
setrawcookie("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