当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > setrawcookie() 函数
The setrawcookie() function sends an HTTP cookie without URL encoding the cookie value.
setrawcookie()函数的作用是:发送一个其值未经 urlencode 编码的 cookie。
A cookie is a variable, sent by the server to the browser. A cookie is typically a small text file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.
Cookie是一个变量,它是通过服务器发送到浏览器的一小段文本文件,并储存在用户的计算机里;每当相同的计算机请求服务器页面时,先前由服务器发送的cookie会再次传回服务器中。
The name of the cookie is automatically assigned to a variable of the same name. For example, if a cookie was sent with the name "user", a variable is automatically created called $user, containing the cookie value.
Cookie的名称将被自动赋到同名变量中。举个例子来说,如果如果发送的cookie名为“user”,那么变量将自动创建并命名为$user,且它包含了cookie的值。
A cookie must be assigned before any other output is sent to the client.
Cookie值必须在任何结果发送到客户端之前指定。
This function returns TRUE on success or FALSE on failure.
如果函数成功执行,则返回True;如果失败将返回False
setrawcookie(name,value,expire,path,domain,secure) |
Parameter参数 | Description描述 |
---|---|
name | Required. Specifies the name of the cookie 必要参数。指定cookie的名称 |
value | Required. Specifies the value of the cookie 必要参数。指定cookie值 |
expire | Optional. Specifies when the cookie expires. 可选参数。指定cookie的过期时间 time()+3600*24*30 will set the cookie to expire in 30 days. If this parameter is not set, the cookie will expire at the end of the session (when the browser closes). |
path | Optional. Specifies the server path of the cookie 可选参数。指定cookie所在的服务器路径 If set to "/", the cookie will be available within the entire domain. If set to "/test/", the cookie will only be available within the test directory and all sub-directories of test. The default value is the current directory that the cookie is being set in. |
domain | Optional. Specifies the domain name of the cookie. 可选参数。指定cookie的域名: To make the cookie available on all subdomains of example.com then you'd set it 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. 可选参数。指定cookie是否需要在HTTPS连接下传输。如果cookie需要在HTTPS连接下传输,则设置为True;否则设置为False。默认值为False |
Tip: The value of a cookie named "user" can be accessed by $HTTP_COOKIE_VARS["user"] or by $_COOKIE["user"].
提示:你可以通过$HTTP_COOKIE_VARS["user"] 或 $_COOKIE["user"]访问这个名为“user”的cookie值。
Note: The setrawcookie() function is exactly the same as setcookie() except that the cookie value will not be automatically URL encoded when sent to the client.
注意:setrawcookie()函数和setcookie()函数极为类似。唯一的不同点是:使用setrawcookie()函数时,发送到客户端的cookie将不会被自动进行URL编码。
Set and send cookie examples:
设置并发送cookie:
<?php $value = "my cookie value"; // send a simple cookie setrawcookie("TestCookie",$value); ?> <html> <body> ... ... |
<?php $value = "my cookie value"; // send a cookie that expires in 24 hours setrawcookie("TestCookie",$value, time()+3600*24); ?> <html> <body> ... ... |
Different ways of retrieving the value of the cookie (after the cookie has been set):
获取cookie值的不同方法(此时,cookie已设置完毕):
<html> <body> <?php // Print individual cookies echo $_COOKIE["TestCookie"]; echo "<br />"; echo $HTTP_COOKIE_VARS["TestCookie"]; echo "<br />"; // Print all cookies print_r($_COOKIE); ?> </body> </html> |
The output of the code above will be:
上述代码将输出下面的结果:
my cookie value my cookie value Array ([TestCookie] => my cookie value) |
Delete a cookie by setting the expiration date to a date/time in the past:
通过在date/time[日期/时间]中指定过期时间来删除cookie,具体如下:
<?php <html> <body> ... ... |
Create an array cookie:
创建一个数组形式的cookie:
<?php setrawcookie("cookie[three]","cookiethree"); setrawcookie("cookie[two]","cookietwo"); setrawcookie("cookie[one]","cookieone"); // print cookies (after reloading page) if (isset($_COOKIE["cookie"])) { foreach ($_COOKIE["cookie"] as $name => $value) { echo "$name : $value <br />"; } } ?> <html> <body> ... ... |
The output of the code above will be:
上述代码将输出下面的结果:
three : cookiethree two : cookietwo one : cookieone |