Hàm setcookie() trong PHP
Ví dụ
Ví dụ sau tạo cookie có tên "người dùng" với giá trị "John Doe". Cookie sẽ hết hạn sau 30 ngày (86400 * 30). "/" có nghĩa là cookie có sẵn trên toàn bộ trang web (nếu không, hãy chọn thư mục bạn thích).
Sau đó, chúng tôi truy xuất giá trị của "người dùng" cookie (sử dụng biến toàn cục $_COOKIE). Chúng tôi cũng sử dụng hàm isset() để tìm hiểu xem cookie có được đặt hay không:
<!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>
Hãy tự mình thử »Định nghĩa và cách sử dụng
Hàm setcookie() xác định một cookie sẽ được gửi cùng với phần còn lại của tiêu đề HTTP.
Cookie thường được sử dụng để nhận dạng người dùng. Cookie là một tệp nhỏ mà máy chủ nhúng vào máy tính của người dùng. Mỗi lần máy tính đó yêu cầu một trang bằng trình duyệt, nó cũng sẽ gửi cookie. Với PHP, bạn có thể vừa tạo vừa truy xuất các giá trị cookie.
Tên của cookie được tự động gán cho một biến có cùng tên. Ví dụ: nếu một cookie được gửi với tên "người dùng", một biến sẽ tự động được tạo có tên là $user, chứa giá trị cookie.
Lưu ý: Hàm setcookie() phải xuất hiện TRƯỚC thẻ <html>.
Lưu ý: Giá trị của cookie được tự động mã hóa URL khi gửi cookie và tự động giải mã khi nhận được (để ngăn chặn mã hóa URL, thay vào đó hãy sử dụng setrawcookie() ).
Cú pháp
setcookie( name, value, expire, path, domain, secure, httponly );
Giá trị tham số
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 |
Chi tiết kỹ thuật
Giá trị trả về: | ĐÚNG khi thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 4+ |
Nhật ký thay đổi PHP: | PHP 5.5 - Thuộc tính Max-Age được bao gồm trong tiêu đề Set-Cookie được gửi tới máy khách PHP 5.2 - Tham số httponly đã được thêm |
Thêm ví dụ
Ví dụ
Một số ngày hết hạn của cookie:
<?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>
Hãy tự mình thử »Ví dụ
Để sửa đổi cookie, chỉ cần đặt (lại) cookie bằng hàm 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>
Hãy tự mình thử »Ví dụ
Để xóa cookie, hãy sử dụng hàm setcookie() có ngày hết hạn trong quá khứ:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Hãy tự mình thử »Ví dụ
Tạo một tập lệnh nhỏ để kiểm tra xem cookie có được bật hay không. Trước tiên, hãy thử tạo cookie thử nghiệm bằng hàm setcookie(), sau đó đếm biến mảng $_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>
Hãy tự mình thử »❮ Tham chiếu mạng PHP