บทช่วยสอนPHP

PHP HOME แนะนำ PHP การติดตั้ง PHP PHP ไวยากรณ์ ความคิดเห็น PHP ตัวแปร PHP PHP Echo / พิมพ์ ประเภทข้อมูล PHP PHP Strings หมายเลข PHP PHP คณิตศาสตร์ ค่าคงที่ PHP ตัวดำเนินการ PHP PHP If...Else...Elseif PHP Switch PHP Loops ฟังก์ชัน PHP PHP Arrays PHP Superglobals PHP RegEx

แบบฟอร์มPHP

การจัดการแบบฟอร์ม PHP การตรวจสอบแบบฟอร์ม PHP ต้องใช้แบบฟอร์ม PHP URL แบบฟอร์ม PHP/อีเมล แบบฟอร์ม PHP เสร็จสมบูรณ์

PHPขั้นสูง

วันที่และเวลา PHP รวม PHP การจัดการไฟล์ PHP ไฟล์ PHP เปิด/อ่าน สร้าง/เขียนไฟล์ PHP อัพโหลดไฟล์ PHP คุกกี้ PHP PHP Sessions ตัวกรอง PHP ตัวกรอง PHP ขั้นสูง ฟังก์ชันเรียกกลับของ PHP PHP JSON ข้อยกเว้น PHP

PHP OOP

PHP OOP คืออะไร คลาส PHP/วัตถุ ตัวสร้าง PHP PHP Destructor PHP Access Modifiers PHP Inheritance ค่าคงที่ PHP PHP Abstract Classes อินเทอร์เฟซ PHP PHP ลักษณะ PHP Static Methods PHP คุณสมบัติคงที่ PHP เนมสเปซ PHP Iterables

ฐานข้อมูลMySQL

ฐานข้อมูล MySQL MySQL Connect MySQL สร้าง DB MySQL สร้างตาราง MySQL แทรกข้อมูล MySQL รับ ID ล่าสุด MySQL แทรกหลายรายการ MySQL ที่เตรียมไว้ MySQL Select Data MySQL Where MySQL สั่งซื้อโดย MySQL ลบข้อมูล ข้อมูลอัพเดต MySQL MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - รับ PHP XML Expat PHP XML DOM

PHP - AJAX

บทนำ AJAX AJAX PHP ฐานข้อมูล AJAX AJAX XML AJAX ค้นหาสด AJAX โพล

ตัวอย่างPHP

ตัวอย่าง PHP PHP คอมไพเลอร์ แบบทดสอบ PHP แบบฝึกหัด PHP ใบรับรอง PHP

ข้อมูลอ้างอิงPHP

ภาพรวม PHP PHP Array ปฏิทิน PHP PHP วันที่ ไดเรกทอรี PHP ข้อผิดพลาด PHP ข้อยกเว้น PHP ระบบไฟล์ PHP ตัวกรอง PHP PHP FTP PHP JSON คีย์เวิร์ด PHP PHP Libxml PHP Mail PHP คณิตศาสตร์ PHP เบ็ดเตล็ด PHP MySQLi เครือข่าย PHP การควบคุมเอาต์พุต PHP PHP RegEx PHP SimpleXML PHP Stream PHP String การจัดการตัวแปร PHP PHP XML Parser PHP Zip เขตเวลา PHP

PHP setcookie()ฟังก์ชั่น

❮ การอ้างอิงเครือข่าย PHP

ตัวอย่าง

ตัวอย่างต่อไปนี้สร้างคุกกี้ชื่อ "ผู้ใช้" โดยมีค่าเป็น "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 คุณสามารถสร้างและดึงค่าคุกกี้ได้

ชื่อของคุกกี้ถูกกำหนดโดยอัตโนมัติให้กับตัวแปรที่มีชื่อเดียวกัน ตัวอย่างเช่น หากส่งคุกกี้ในชื่อ "ผู้ใช้" ตัวแปรจะถูกสร้างขึ้นโดยอัตโนมัติเรียกว่า $user โดยมีค่าคุกกี้

หมายเหตุ:ฟังก์ชัน setcookie() จะต้องปรากฏก่อนแท็ก <html>

หมายเหตุ:ค่าของคุกกี้จะถูกเข้ารหัส URL โดยอัตโนมัติเมื่อส่งคุกกี้ และจะถูกถอดรหัสโดยอัตโนมัติเมื่อได้รับ (เพื่อป้องกันการเข้ารหัส 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


รายละเอียดทางเทคนิค

คืนมูลค่า: จริงกับความสำเร็จ 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