Hướng dẫn how can you create access and delete a cookie in php with example? - Làm thế nào bạn có thể tạo quyền truy cập và xóa cookie trong php với ví dụ?

Trong hướng dẫn này, bạn sẽ tìm hiểu cách lưu trữ một lượng nhỏ thông tin trong bản thân trình duyệt của người dùng bằng cách sử dụng cookie PHP.

Cookie là một tệp văn bản nhỏ cho phép bạn lưu trữ một lượng nhỏ dữ liệu (gần 4kb) trên máy tính của người dùng. They are typically used to keeping track of information such as username that the site can retrieve to personalize the page when user visit the website next time.

Mẹo: Mỗi lần trình duyệt yêu cầu một trang đến máy chủ, tất cả dữ liệu trong cookie sẽ tự động gửi đến máy chủ trong yêu cầu. Each time the browser requests a page to the server, all the data in the cookie is automatically sent to the server within the request.

Hàm setcookie() được sử dụng để đặt cookie trong PHP. Đảm bảo bạn gọi hàm setcookie() trước khi bất kỳ đầu ra nào được tạo bởi tập lệnh của bạn nếu không cookie sẽ không được đặt. Cú pháp cơ bản của chức năng này có thể được đưa ra với:

setCookie (tên, giá trị, hết hạn, đường dẫn, tên miền, an toàn);(name, value, expire, path, domain, secure);

Các tham số của hàm setcookie() có ý nghĩa sau:

Tham sốSự mô tả
nameTên của cookie.
________số 8Giá trị của cookie. Không lưu trữ thông tin nhạy cảm vì giá trị này được lưu trữ trên máy tính của người dùng.
expiresNgày hết hạn ở định dạng dấu thời gian UNIX. Sau thời gian này, Cookie sẽ không thể tiếp cận được. Giá trị mặc định là 0.
0
Chỉ định đường dẫn trên máy chủ mà cookie sẽ có sẵn. Nếu được đặt thành
1, cookie sẽ có sẵn trong toàn bộ miền.
2
Chỉ định tên miền mà cookie có sẵn cho www.example.com.
3
Trường này, nếu có, chỉ ra rằng cookie chỉ nên được gửi nếu kết nối HTTPS an toàn tồn tại.

Tip: If the expiration time of the cookie is set to 0, or omitted, the cookie will expire at the end of the session i.e. when the browser closes. If the expiration time of the cookie is set to 0, or omitted, the cookie will expire at the end of the session i.e. when the browser closes.

Dưới đây là một ví dụ sử dụng hàm setcookie() để tạo cookie có tên

5 và gán giá trị giá trị
6 cho nó. Nó cũng chỉ định rằng cookie sẽ hết hạn sau 30 ngày (
7).

Lưu ý: Tất cả các đối số ngoại trừ tên là tùy chọn. You may also replace an argument with an empty string ("") in order to skip that argument, however to skip the expire argument use a zero (0) instead, since it is an integer. All the arguments except the name are optional. You may also replace an argument with an empty string ("") in order to skip that argument, however to skip the expire argument use a zero (0) instead, since it is an integer.

CẢNH BÁO: Không lưu trữ dữ liệu nhạy cảm trong cookie vì nó có khả năng bị người dùng độc hại thao túng. Để lưu trữ các phiên sử dụng dữ liệu nhạy cảm thay thế. Don't store sensitive data in cookies since it could potentially be manipulated by the malicious user. To store the sensitive data securely use sessions instead.


Biến Superglobal PHP

8 được sử dụng để lấy giá trị cookie. It typically an associative array that contains a list of all the cookies values sent by the browser in the current request, keyed by cookie name. The individual cookie value can be accessed using standard array notation, for example to display the username cookie set in the previous example, you could use the following code.

Mã PHP trong ví dụ trên tạo ra đầu ra sau.

John Carter

Đó là một thực tế tốt để kiểm tra xem cookie có được đặt hay không trước khi truy cập giá trị của nó. Để làm điều này, bạn có thể sử dụng chức năng PHP

9, như thế này:

Bạn có thể sử dụng chức năng

0 như
1 để xem cấu trúc của mảng liên kết
8 này, giống như bạn với các mảng khác.


You can delete a cookie by calling the same setcookie() function with the cookie name and any value (such as an empty string) however this time you need the set the expiration date in the past, as shown in the example below:

Tip: You should pass exactly the same path, domain, and other arguments that you have used when you first created the cookie in order to ensure that the correct cookie is deleted. You should pass exactly the same path, domain, and other arguments that you have used when you first created the cookie in order to ensure that the correct cookie is deleted.


Một cookie thường được sử dụng để xác định 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 cùng một máy tính yêu cầu một trang với trình duyệt, nó cũng sẽ gửi cookie. Với PHP, bạn có thể tạo và truy xuất các giá trị cookie.


Một cookie được tạo với chức năng setcookie().

Cú pháp

setCookie (tên, giá trị, hết hạn, đường dẫn, tên miền, an toàn, httponly);

Chỉ cần tham số tên. Tất cả các tham số khác là tùy chọn.


Ví dụ sau đây tạo ra một 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 trong toàn bộ trang web (nếu không, chọn thư mục bạn thích).

Sau đó, chúng tôi lấy giá trị của cookie "người dùng" (sử dụng biến toàn cầu $ _cookie). Chúng tôi cũng sử dụng chức năng

9 để tìm hiểu xem cookie có được đặt không:

Thí dụ

$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

if(!isset($_COOKIE[$cookie_name])) {
  echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
  echo "Cookie '" . $cookie_name . "' is set!
";
  echo "Value is: " . $_COOKIE[$cookie_name];
}
?>


Chạy ví dụ »

Lưu ý: Hàm setcookie() phải xuất hiện trước thẻ. The setcookie() function must appear BEFORE the tag.

Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use

7 instead). The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use
7 instead).



Để sửa đổi cookie, chỉ cần đặt (một lần nữa) cookie bằng hàm setcookie():

Thí dụ

$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>

if(!isset($_COOKIE[$cookie_name])) {
  echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
  echo "Cookie '" . $cookie_name . "' is set!
";
  echo "Value is: " . $_COOKIE[$cookie_name];
}
?>


Chạy ví dụ »


Để xóa cookie, hãy sử dụng chức năng setcookie() với ngày hết hạn trong quá khứ:

Thí dụ

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

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


Chạy ví dụ »


Để xóa cookie, hãy sử dụng chức năng setcookie() với ngày hết hạn trong quá khứ:

Thí dụ

setcookie("test_cookie", "test", time() + 3600, '/');
?>

Chạy ví dụ »
if(count($_COOKIE) > 0) {
  echo "Cookies are enabled.";
} else {
  echo "Cookies are disabled.";
}
?>


Chạy ví dụ »


Để xóa cookie, hãy sử dụng chức năng setcookie() với ngày hết hạn trong quá khứ:




Ví dụ 1: Bạn có thể tạo cookie bằng cách viết setCookie () và nhập ngày hết hạn của cookie. Nếu bạn muốn xóa cookie thì hãy đặt ngày hết hạn cookie thành thời gian hiện tại. Nếu bạn muốn hiển thị cookie thì bạn có thể lặp lại cookie bằng $ _cookie ['name'] và nó sẽ in chi tiết cookie.create the cookies by writing setcookie() and entering the expiry date of the cookie. If you want to delete the cookie then set the cookie expiry date to the current time. If you want to display the cookie then you can echo the cookie by $_cookie['name'] and it will print the cookie details.
Sửa đổi giá trị cookie. Để sửa đổi cookie, chỉ cần đặt (một lần nữa) cookie bằng hàm setCookie (): ....
Xóa cookie. Để xóa cookie, hãy sử dụng hàm setCookie () với ngày hết hạn trong quá khứ: ....
Kiểm tra xem cookie có được bật không. Ví dụ sau đây tạo ra một tập lệnh nhỏ kiểm tra xem cookie có được bật không ..
Cookies là các tệp văn bản với các đoạn dữ liệu nhỏ - như tên người dùng và mật khẩu - được sử dụng để xác định máy tính của bạn khi bạn sử dụng mạng máy tính. Cookies cụ thể được gọi là cookie HTTP được sử dụng để xác định người dùng cụ thể và cải thiện trải nghiệm duyệt web của bạn.text files with small pieces of data — like a username and password — that are used to identify your computer as you use a computer network. Specific cookies known as HTTP cookies are used to identify specific users and improve your web browsing experience.
Sự khác biệt giữa phiên và cookie.