Cookie通常用来验证或辨别一个用户。 Cookie是什么呢? Cookie通常用来验证或辨别一个用户。Cookie是通过服务器发送到用户计算机中的一个小文件。每次,当相同的计算机通过浏览器请求一个页面时,原先存储的cookie也会发送到服务器。你可以使用PHP来创建和获
Cookie通常用来验证或辨别一个用户。
Cookie是什么呢?
Cookie通常用来验证或辨别一个用户。Cookie是通过服务器发送到用户计算机中的一个小文件。每次,当相同的计算机通过浏览器请求一个页面时,原先存储的cookie也会发送到服务器。你可以使用PHP来创建和获取cookie的值。
怎样创建一个cookie呢?
Setcookie()函数是用来设置一个cookie的。
语法
setcookie(name, value, expire, path, domain);
案例
在下面这个例子中,我们将创建一个名为“user”的cookie并把“Alex Porter”这个值赋给它;同时,我们还规定cookie将在一小时后过期:
setcookie("user", "Alex Porter", time()+3600);
?>
注意:当发送cookie时,cookie值是自动进行URL编码[URLencoded]的;当受到cookie时,自动进行URL解码。(如果你不希望进行URL编码,可以使用setrawcookie()函数替代)
如何获取cookie值?
PHP $_COOKIE变量是用来获取一个cookie值的
在下面这个例子中,我们获取了名为“user”的值并将它显示在页面上:
print_r($_COOKIE);
?>
我们使用isset()函数来检查cookie是否被设置:
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!
";
else
echo "Welcome guest!
";
?>
如何删除一个cookie?
当你要删除一个cookie时,你必须确保cookie已经过期。
案例:
// set the expiration date to one hour agosetcookie("user", "", time()-3600);
?>
如果浏览器不支持Cookie怎么办?
如果你的浏览器不支持cookie,你必须使用其他方法将一个页面的信息传输到另一页面中。其中一个方法就是使用表单(我们已经在先前的教程中提到过)。
下面这个案例:当用户点击“提交submit”按钮的时候,用户输入的信息被传输到“welcome.php”:
下面的案例指明了如何获取“welcome.php”文件中的值:
Welcome .
You are years old.

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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.