当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > setrawcookie() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 setrawcookie() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 581 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

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

Syntax
语法

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).
“time()+3600*24*30”将cookie的过期时间设置为30天。如果这个参数没有设置,那么cookie将在session结束后(即:浏览器关闭时)自动失效

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.
如果路径设置“/”,那么cookie将在整个域名内有效;如果路径设置为“/test/”,那么cookie值在“test”目录下或其子目录下有效;默认的路径是cookie所处的当前目录

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
当你把cookie的域名设置为“.example.com”时,cookie在“.example.com”的所有子域名中有效;当你把cookie的域名设置为“www.example.com”时,cookie在“www”的所有子域名中有效

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


Tips and Notes
提示和注意点

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编码。


Example 1
案例1

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>
...
...


Example 2
案例2

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)


Example 3
案例3

Delete a cookie by setting the expiration date to a date/time in the past:
通过在date/time[日期/时间]中指定过期时间来删除cookie,具体如下:

<?php
// Set the expiration date to one hour ago
setrawcookie ("TestCookie", "", time() - 3600);
?>
<html>
<body>
...
...


Example 4
案例4

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

评论 (0) All

登陆 | 还没注册?