A cookie is a small piece of data stored on the user's computer by the web browser. In PHP, you can set and retrieve cookies using the setcookie() function.
To set a cookie, you can use the following syntax:
setcookie(name, value, expire, path, domain, secure, httponly);
name: The name of the cookie.value: The value of the cookie.expire: The expiration time of the cookie. It is a Unix timestamp, so you can use time() function to set the current time or add a specific number of seconds to set the expiration time.path: The path on the server where the cookie will be available. If set to "/", the cookie will be available for the entire domain.domain: The domain where the cookie will be available. Use an empty string or null to make it available on the current domain.secure: If set to true, the cookie will only be transmitted over a secure HTTPS connection.httponly: If set to true, the cookie will only be accessible through the HTTP protocol and not through client-side scripting languages like JavaScript.Here's an example of setting a cookie:
setcookie("username", "John Doe", time() + 3600, "/");
To retrieve a cookie, you can use the $_COOKIE superglobal variable. It is an associative array where the keys are the cookie names and the values are the cookie values.
$username = $_COOKIE["username"];
Note that cookies are sent as HTTP headers, so you need to set them before any output is sent to the browser.
上一篇:php数字转换教程
下一篇:php 数组倒叙排序
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站