PHP setrawcookie() İşlevi
Örnek
Aşağıdaki örnek, PHP ile bir çerez oluşturur. Çerezin adı "user" ve değeri "John Doe" olacaktır. Çerez değeri, URL kodlamalı olmayacaktır. Çerez, 30 gün sonra (86400 * 30) sona erecektir. "/" kullanılması, çerezin tüm web sitesinde mevcut olduğu anlamına gelir (aksi takdirde, tercih ettiğiniz dizini seçin):
<?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>
?>
Tanım ve Kullanım
setrawcookie() işlevi, HTTP üstbilgilerinin geri kalanıyla birlikte gönderilecek bir çerezi (URL kodlaması olmadan) tanımlar.
Bir kullanıcıyı tanımlamak için genellikle bir çerez kullanılır. Çerez, sunucunun kullanıcının bilgisayarına yerleştirdiği küçük bir dosyadır. Aynı bilgisayar tarayıcılı bir sayfa istediğinde, çerezi de gönderir. PHP ile tanımlama bilgisi değerlerini hem oluşturabilir hem de alabilirsiniz.
Çerezin adı, aynı ada sahip bir değişkene otomatik olarak atanır. Örneğin, "kullanıcı" adıyla bir tanımlama bilgisi gönderildiyse, tanımlama bilgisi değerini içeren $user adlı bir değişken otomatik olarak oluşturulur.
Not: setrawcookie() işlevi <html> etiketinden ÖNCE görünmelidir.
Not: Çerez değerini gönderirken otomatik olarak URL kodlaması yapmak ve alırken otomatik olarak kodunu çözmek için bunun yerine setcookie() işlevini kullanın.
Sözdizimi
setrawcookie(name, value, expire, path, domain, secure);
Parametre Değerleri
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. |
Teknik detaylar
Geri dönüş değeri: | Başarı konusunda DOĞRU. Başarısızlıkta YANLIŞ |
---|---|
PHP Sürümü: | 5+ |
Daha fazla örnek
Örnek
"user" adlı çerezin değerini alın ($_COOKIE global değişkenini kullanarak). Çerezin var olup olmadığını öğrenmek için isset() işlevini de kullanın:
<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>
Örnek
Bir tanımlama bilgisini değiştirmek için, tanımlama bilgisini setrawcookie() işlevini kullanarak (tekrar) ayarlayın:
<?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>
Örnek
Bir tanımlama bilgisini silmek için, geçmişte sona erme tarihi olan setrawcookie() işlevini kullanın:
<?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>
Örnek
Çerezlerin etkinleştirilip etkinleştirilmediğini kontrol eden küçük bir komut dosyası oluşturun. İlk önce setrawcookie() işleviyle bir test tanımlama bilgisi oluşturmayı deneyin, ardından $_COOKIE dizi değişkenini sayın:
<?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 Ağ Referansı