当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP Cookies

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
用PHP5的DirectoryIterators递归扫描目录
PHP 阻止SQL注入式攻击
PHP5面向对象 - 基础 - 类和对象
PHP5面向对象 - 基础 - 类的属性( public )
PHP5面向对象 - 基础 - 类的属性( private )
PHP5面向对象 - 基础 - 方法
PHP5面向对象 - 基础 - 对象的比较
php5面向对象 - 基础 - 构造函数
php5面向对象 - 基础 - 析构函数
用PHP控制用户的浏览器 - ob*函数的使用
PHP PDO 学习笔记

PHP Cookies


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

A cookie is often used to identify a user.
Cookie通常用来验证或辨别一个用户。


What is a Cookie?
Cookie是什么呢?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
Cookie通常用来验证或辨别一个用户。Cookie是通过服务器发送到用户计算机中的一个小文件。每次,当相同的计算机通过浏览器请求一个页面时,原先存储的cookie也会发送到服务器。你可以使用PHP来创建和获取cookie的值。


How to Create a Cookie?
怎样创建一个cookie呢?

The setcookie() function is used to set a cookie.
Setcookie()函数是用来设置一个cookie的。

Note: The setcookie() function must appear BEFORE the <html> tag.
 注意:setcookie()函数必须在<html>标签之前。

Syntax
语法

setcookie(name, value, expire, path, domain);

Example
案例

In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:
在下面这个例子中,我们将创建一个名为“user”的cookie并把“Alex Porter”这个值赋给它;同时,我们还规定cookie将在一小时后过期:

<?php
setcookie("user", "Alex Porter", time()+3600);
?>
<html>
<body>
</body>
</html>

Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
注意:当发送cookie时,cookie值是自动进行URL编码[URLencoded]的;当受到cookie时,自动进行URL解码。(如果你不希望进行URL编码,可以使用setrawcookie()函数替代)


How to Retrieve a Cookie Value?
如何获取cookie值?

The PHP $_COOKIE variable is used to retrieve a cookie value.
PHP $_COOKIE变量是用来获取一个cookie值的

In the example below, we retrieve the value of the cookie named "user" and display it on a page:
在下面这个例子中,我们获取了名为“user”的值并将它显示在页面上:

<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>

In the following example we use the isset() function to find out if a cookie has been set:
我们使用isset()函数来检查cookie是否被设置:

<html>
<body>
<?php
if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />";
else echo "Welcome guest!<br />";
?>
</body>
</html>


How to Delete a Cookie?
如何删除一个cookie?

When deleting a cookie you should assure that the expiration date is in the past.
当你要删除一个cookie时,你必须确保cookie已经过期。

Delete example:
案例:

<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600); ?>


What if a Browser Does NOT Support Cookies?
如果浏览器不支持Cookie怎么办?

If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. One method is to pass the data through forms (forms and user input are described earlier in this tutorial).
如果你的浏览器不支持cookie,你必须使用其他方法将一个页面的信息传输到另一页面中。其中一个方法就是使用表单(我们已经在先前的教程中提到过)。

The form below passes the user input to "welcome.php" when the user clicks on the "Submit" button:
下面这个案例:当用户点击“提交submit”按钮的时候,用户输入的信息被传输到“welcome.php”:

<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

Retrieve the values in the "welcome.php" file like this:
下面的案例指明了如何获取“welcome.php”文件中的值:

<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>

评论 (0) All

登陆 | 还没注册?