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. Show
Cookie là gì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. Đặt cookie trong PHPHàm 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
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 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. Truy cập giá trị cookieBiế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.Loại bỏ cookieYou can delete a cookie by calling the same
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. Cookie là gì?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. Tạo cookie với PHPMột cookie được tạo với chức năng Cú phápsetCookie (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. PHP tạo/truy xuất cookieVí 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"; if(!isset($_COOKIE[$cookie_name])) { Chạy ví dụ » Lưu ý: Hàm Note: 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 giá trị cookieĐể sửa đổi cookie, chỉ cần đặt (một lần nữa) cookie bằng hàm Thí dụ$cookie_name = "user"; if(!isset($_COOKIE[$cookie_name])) { Chạy ví dụ » Xóa cookieĐể xóa cookie, hãy sử dụng chức năng Thí dụ// set the expiration date to one hour
ago echo "Cookie 'user' is deleted."; Chạy ví dụ » Xóa cookieĐể xóa cookie, hãy sử dụng chức năng Thí dụsetcookie("test_cookie", "test", time() + 3600, '/'); Chạy ví dụ » Chạy ví dụ » Xóa cookieĐể xóa cookie, hãy sử dụng chức năng Kiểm tra xem cookie có được bật khôngLàm thế nào bạn có thể tạo quyền truy cập và xóa cookie trong PHP với sự trợ giúp của một ví dụ?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.
Làm thế nào một cookie được tạo và xóa bằng PHP?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 .. Cookie giải thích với ví dụ là gì?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.
Phiên và cookie trong PHP với ví dụ là gì?Sự khác biệt giữa phiên và cookie. |