


php setcookie(name, value, expires, path, domain, secure) parameters_PHP tutorial
setcookie() 定义一个和其余的 HTTP 标头一起发送的 cookie。和其它标头一样,cookie 必须在脚本的任何其它输出之前发送(这是协议限制)。这需要将本函数的调用放到任何输出之前,包括 和
标签以及任何空格。如果在调用 setcookie() 之前有任何输出,本函数将失败并返回 FALSE。如果 setcookie() 函数成功运行,将返回 TRUE。这并不说明用户是否接受了 cookie。函数定义:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )
setcookie() 参数详解
参数 | 说明 | 举例 |
name | cookie的名字 | 使用 $_COOKIE['cookiename'] 调用名为 cookiename 的 cookie。 |
value | cookie的值,存放在客户端,不要存放敏感数据 | 假定 name 是 'cookiename',可以通过$_COOKIE['cookiename'] 取得其值。 |
expire |
Cookie 过期的时间。这是个 Unix 时间戳,即从 Unix 纪元开始的秒数。 换而言之,通常用 time() 函数再加上秒数来设定 cookie 的失效期。 或者用mktime()来实现。 |
time()+60*60*24*30 将设定 cookie 30 天后失效。 如果未设定,cookie 将会在会话结束后(一般是浏览器关闭)失效。 |
path | Cookie 在服务器端的有效路径。 |
如果该参数设为 '/' 的话,cookie 就在整个 domain 内有效, 如果设为 '/foo/',cookie 就只在 domain 下的 /foo/ 目录及其子目录内有效,例如 /foo/bar/。 默认值为设定 cookie 的当前目录。 |
domain | 该 cookie 有效的域名。 |
要使 cookie 能在如 example.com 域名下的所有子域都有效的话,该参数应该设为 '.example.com'。 虽然 . 并不必须的,但加上它会兼容更多的浏览器。 如果该参数设为www.example.com 的话,就只在 www 子域内有效。 细节见Cookie 规范中的 tail matching。 |
secure |
指明 cookie 是否仅通过安全的 HTTPS 连接传送。 当设成 TRUE 时,cookie 仅在安全的连接中被设置。默认值为FALSE。 |
0 或 1 |
Example 1. setcookie() sending example
$ value = 'something from somewhere';
setcookie("TestCookie", $value);
setcookie("TestCookie", $value,time()+3600); /* expire in 1 hour */
setcookie("TestCookie", $value,time()+3600, "/~rasmus/", ".utoronto.ca", 1);
Pay attention to the value of the cookie The part will be automatically urlencoded when sent and automatically decoded when received and assigned the value to the cookie variable with the same name. If you don't want this and are using PHP 5, you can use setrawcookie() instead. The following simple example can get the value of the cookie just set:
// Output a separate cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];
// Another debugging method is to output All cookies
print_r($_COOKIE);
?>
To delete a cookie, you need to ensure that its expiration date is in the past to trigger the browser's deletion mechanism. The following example illustrates how to delete the cookie just set:
Example 2. setcookie() Delete example
//Set the expiration time to one hour ago
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", " ", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);
You can also set an array cookie by using the array symbol in the cookie name. Multiple cookies can be set as array units. When the script extracts cookies, all values are placed in an array:
Example 3. Example of using arrays in setcookie()
// Set cookie
setcookie("cookie[three]" , "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// After refreshing the page, it will be displayed out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
echo "$name : $value
n";
}
}
?>
The above example will output:
three : cookiethree
two : cookietwo
one : cookieone
Summary: The basic use of cookies is not difficult. The focus of this article is mainly to master the path setting and domain of the path. domain name settings.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
