PHP handleiding

PHP TUIS PHP Inleiding PHP installeer PHP sintaksis PHP opmerkings PHP veranderlikes PHP Echo / Druk PHP datatipes PHP Strings PHP-nommers PHP Wiskunde PHP konstante PHP-operateurs PHP As...Anders...Elseif PHP skakelaar PHP-lusse PHP funksies PHP-skikkings PHP Superglobals PHP RegEx

PHP- vorms

PHP-vormhantering PHP-vorm validering PHP-vorm word vereis PHP-vorm URL/e-pos PHP-vorm voltooi

PHP Gevorderd

PHP datum en tyd PHP sluit in PHP-lêerhantering PHP-lêer oop/lees PHP-lêer skep/skryf PHP-lêer oplaai PHP-koekies PHP-sessies PHP filters PHP-filters Gevorderd PHP-terugbelfunksies PHP JSON PHP-uitsonderings

PHP OOP

PHP Wat is OOP PHP Klasse/Objekte PHP Konstrukteur PHP vernietiger PHP Toegangswysigers PHP erfenis PHP konstante PHP Abstrakte Klasse PHP-koppelvlakke PHP eienskappe PHP statiese metodes PHP Statiese Eienskappe PHP naamruimtes PHP Iterables

MySQL- databasis

MySQL-databasis MySQL Connect MySQL Skep DB MySQL Skep tabel MySQL Voeg data in MySQL Kry Laaste ID MySQL Voeg veelvuldige in MySQL voorberei MySQL Kies Data MySQL Waar MySQL Bestel deur MySQL verwyder data MySQL-opdateringsdata MySQL-limietdata

PHP XML

PHP XML-ontleders PHP SimpleXML-ontleder PHP SimpleXML - Kry PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX-databasis AJAX XML AJAX Live Search AJAX-peiling

PHP voorbeelde

PHP voorbeelde PHP samesteller PHP vasvra PHP Oefeninge PHP-sertifikaat

PHP- verwysing

PHP Oorsig PHP Skikking PHP-kalender PHP datum PHP gids PHP fout PHP-uitsondering PHP lêerstelsel PHP filter PHP FTP PHP JSON PHP sleutelwoorde PHP Libxml PHP-pos PHP Wiskunde PHP Diverse PHP MySQLi PHP-netwerk PHP-uitsetbeheer PHP RegEx PHP SimpleXML PHP-stroom PHP-string PHP veranderlike hantering PHP XML-ontleder PHP zip PHP Tydsones

PHP setcookie() Funksie

❮ PHP-netwerkverwysing

Voorbeeld

Die volgende voorbeeld skep 'n koekie met die naam "gebruiker" met die waarde "John Doe". Die koekie sal na 30 dae verval (86400 * 30). Die "/" beteken dat die koekie beskikbaar is in die hele webwerf (anders kies die gids wat jy verkies).

Ons haal dan die waarde van die koekie "gebruiker" op (met die globale veranderlike $_COOKIE). Ons gebruik ook die isset()-funksie om uit te vind of die koekie gestel is:

<!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>

Definisie en gebruik

Die setcookie()-funksie definieer 'n koekie wat saam met die res van die HTTP-opskrifte gestuur moet word.

'n Koekie word dikwels gebruik om 'n gebruiker te identifiseer. 'n Koekie is 'n klein lêer wat die bediener op die gebruiker se rekenaar insluit. Elke keer as dieselfde rekenaar 'n bladsy met 'n blaaier versoek, sal dit ook die koekie stuur. Met PHP kan jy koekiewaardes skep en herwin.

Die naam van die koekie word outomaties aan 'n veranderlike met dieselfde naam toegeken. Byvoorbeeld, as 'n koekie met die naam "gebruiker" gestuur is, word 'n veranderlike outomaties geskep genaamd $user, wat die koekiewaarde bevat.

Let wel: Die setcookie()-funksie moet VOOR die <html>-merker verskyn.

Let wel: Die waarde van die koekie word outomaties URL-gekodeer wanneer die koekie gestuur word, en outomaties gedekodeer wanneer dit ontvang word (om URL-enkodering te voorkom, gebruik eerder setrawcookie() ).

Sintaksis

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

Parameterwaardes

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


Tegniese besonderhede

Terugkeerwaarde: WAAR oor sukses. ONWAAR op mislukking
PHP weergawe: 4+
PHP Changelog: PHP 5.5 - 'n Max-Age-kenmerk is ingesluit in die Stel-koekie-kopskrif wat na die kliënt gestuur is
PHP 5.2 - Die httponly-parameter is bygevoeg

Meer voorbeelde

Voorbeeld

Verskeie vervaldatums vir koekies:

<?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 'n koekie te verander, stel net (weer) die koekie met behulp van die setcookie()-funksie:

<?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

Om 'n koekie te skrap, gebruik die setcookie()-funksie met 'n vervaldatum in die verlede:

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

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

</body>
</html>

Voorbeeld

Skep 'n klein skrif wat kyk of webkoekies geaktiveer is. Probeer eers om 'n toetskoekie te skep met die setcookie() funksie, tel dan die $_COOKIE skikking veranderlike:

<?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-netwerkverwysing