search
Homephp教程php手册PHP 如何获取Cookies

PHP 如何获取Cookies

Jun 21, 2016 am 08:46 AM
cookienbspquotsetcookieuser


PHP Cookies
 

cookie 常用于识别用户。cookie 是一种服务器留在用户计算机上的小文件。每当同一台计算机通过浏览器请求页面时,这台计算机将会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。 setcookie() 函数用于设置 cookie。 注释:setcookie() 函数必须位于 标签之前。 语法: setcookie(name, value, expire, path, domain); 在下面的例子中,我们将创建名为 "user" 的 cookie,并为它赋值 "Alex Porter"。我们也规定了此 cookie 在一小时后过期:
  1. setcookie("user","AlexPorter",time()+3600); 
  2. ?> 
  3.  
  4.  
  5. ..... 

注释:在发送 cookie 时,cookie 的值会自动进行 URL 编码,在取回时进行自动解码。(为防止 URL 编码,请使用 setrawcookie() 取而代之。) PHP 的 $_COOKIE 变量用于取回 cookie 的值。 在下面的实例中,我们取回了名为 "user" 的 cookie 的值,并把它显示在了页面上:
  1. //Printacookie
  2. echo$_COOKIE["user"]; 
  3.  
  4. //Awaytoviewallcookies
  5. print_r($_COOKIE); 
  6. ?> 
在下面的实例中,我们使用 isset() 函数来确认是否已设置了 cookie:
  1.  
  2.  
  3.  
  4. if(isset($_COOKIE["user"])) 
  5. echo"Welcome".$_COOKIE["user"]."!
    "
  6. else
  7. echo"Welcomeguest!
    "
  8. ?> 
  9.  
  10.  
  11.  


如何删除 Cookie?

当删除 cookie 时,您应当使过期日期变更为过去的时间点。 删除的实例:
  1. //settheexpirationdatetoonehourago 
  2. setcookie("user","",time()-3600);  
  3. ?>  

JavaScript 如何创建、获取Cookies



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor