وظيفة PHP setcookie ()
مثال
يقوم المثال التالي بإنشاء ملف تعريف ارتباط باسم "المستخدم" بقيمة "John Doe". تنتهي صلاحية ملف تعريف الارتباط بعد 30 يومًا (86400 * 30). يعني "/" أن ملف تعريف الارتباط متاح في موقع الويب بالكامل (بخلاف ذلك ، حدد الدليل الذي تفضله).
ثم نسترد قيمة ملف تعريف الارتباط "المستخدم" (باستخدام المتغير العام $ _COOKIE). نستخدم أيضًا الوظيفة isset () لمعرفة ما إذا تم تعيين ملف تعريف الارتباط:
<!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>
التعريف والاستخدام
تحدد الوظيفة setcookie () ملف تعريف الارتباط الذي سيتم إرساله مع بقية رؤوس HTTP.
غالبًا ما يستخدم ملف تعريف الارتباط لتحديد هوية المستخدم. ملف تعريف الارتباط هو ملف صغير يقوم الخادم بتضمينه على كمبيوتر المستخدم. في كل مرة يطلب فيها نفس الكمبيوتر صفحة بها متصفح ، سيرسل ملف تعريف الارتباط أيضًا. باستخدام PHP ، يمكنك إنشاء واسترداد قيم ملفات تعريف الارتباط.
يتم تعيين اسم ملف تعريف الارتباط تلقائيًا إلى متغير بنفس الاسم. على سبيل المثال ، إذا تم إرسال ملف تعريف ارتباط باسم "المستخدم" ، يتم إنشاء متغير تلقائيًا يسمى المستخدم $ ، يحتوي على قيمة ملف تعريف الارتباط.
ملاحظة: يجب أن تظهر الوظيفة setcookie () قبل علامة <html>.
ملاحظة: قيمة ملف تعريف الارتباط يتم تشفيرها تلقائيًا عند إرسال ملف تعريف الارتباط ، ويتم فك تشفيرها تلقائيًا عند استلامها (لمنع تشفير عنوان URL ، استخدم setrawcookie () بدلاً من ذلك).
بناء الجملة
setcookie(name, value, expire, path, domain, secure, httponly);
قيمه المعامل
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 |
تفاصيل تقنية
قيمة الإرجاع: | صواب في النجاح. خطأ في الفشل |
---|---|
إصدار PHP: | 4+ |
سجل التغيير في PHP: | PHP 5.5 - تم تضمين سمة Max-Age في رأس Set-Cookie المرسل إلى العميل PHP 5.2 - تمت إضافة المعلمة httponly |
مزيد من الأمثلة
مثال
عدة تواريخ انتهاء صلاحية لملفات تعريف الارتباط:
<?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>
مثال
لتعديل ملف تعريف الارتباط ، ما عليك سوى تعيين (مرة أخرى) ملف تعريف الارتباط باستخدام وظيفة setcookie ():
<?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>
مثال
لحذف ملف تعريف ارتباط ، استخدم وظيفة setcookie () مع تاريخ انتهاء صلاحية في الماضي:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
مثال
قم بإنشاء برنامج نصي صغير يتحقق من تمكين ملفات تعريف الارتباط. أولاً ، حاول إنشاء ملف تعريف ارتباط تجريبي باستخدام الوظيفة setcookie () ، ثم عد متغير المصفوفة $ _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