A. 曲奇 在PHP中,web服务器存储在客户端计算机上的最大大小为4KB的小文件。它们通常用于跟踪用户名等信息,当用户下次访问网站时,网站可以检索该用户名以对页面进行个性化设置。cookie只能从发布它的域中读取。cookie通常设置在HTTP头中,但JavaScript也可以直接在浏览器上设置cookie。
在PHP中设置Cookie :要在PHP中设置cookie,请 setcookie() 函数被使用。需要在脚本生成任何输出之前调用setcookie()函数,否则将不会设置cookie。
语法:
setcookie(name, value, expire, path, domain, security);
参数: setcookie()函数通常需要六个参数,它们是:
- 姓名: 它用于设置cookie的名称。
- 价值: 它用于设置cookie的值。
- 到期: 它用于设置cookie的到期时间戳,在此时间戳之后,cookie将无法访问。
- 路径: 它用于指定服务器上cookie可用的路径。
- 域名: 它用于指定cookie可用的域。
- 安全: 它用于指示仅当存在安全的HTTPS连接时才应发送cookie。
以下是可以在PHP中对Cookie执行的一些操作:
- 创建Cookies :创建一个名为Auction_Item的cookie,并为其分配豪华车的价值。cookie将在2天后过期(2天*24小时*60分钟*60秒)。
例子: 本例描述了在PHP中创建cookie的过程。
PHP
<!DOCTYPE html> <?php setcookie( "Auction_Item" , "Luxury Car" , time() + 2 * 24 * 60 * 60); ?> <html> <body> <?php echo "cookie is created." ?> <p> <strong>Note:</strong> You might have to reload the page to see the value of the cookie. </p> </body> </html> |
注: 只有setcookie()函数中的name参数是必需的。要跳过参数,可以用空字符串(“”)替换该参数。
输出:
![图片[1]-PHP Cookies-yiteyi-C++库](https://media.geeksforgeeks.org/wp-content/uploads/20211128215212/1.png)
在PHP中创建Cookie
检查是否设置了Cookie :访问cookie值之前,最好先检查cookie是否已设置。因此,要检查是否设置了cookie,需要使用PHP isset()函数。要检查是否设置了cookie“Auction_Item”,请按如下方式执行isset()函数:
例子: 本例描述了检查cookie是否已设置。
PHP
<!DOCTYPE html> <?php setcookie( "Auction_Item" , "Luxury Car" , time() + 2 * 24 * 60 * 60); ?> <html> <body> <?php if (isset( $_COOKIE [ "Auction_Item" ])) { echo "Auction Item is a " . $_COOKIE [ "Auction_Item" ]; } else { echo "No items for auction." ; } ?> <p> <strong>Note:</strong> You might have to reload the page to see the value of the cookie. </p> </body> </html> |
输出:
![图片[2]-PHP Cookies-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/geeks/20211128/geeks_20211128220022.gif)
正在检查要设置的cookie
访问Cookie值 :为了访问cookie值,使用PHP$_cookie超全局变量。它是一个关联数组,包含浏览器在当前请求中发送的所有cookie值的记录。这些记录存储为一个列表,其中cookie名称用作密钥。要访问名为“Auction_Item”的cookie,可以执行以下代码。
例子: 本例描述了访问和修改cookie值。
PHP
<!DOCTYPE html> <?php setcookie( "Auction_Item" , "Luxury Car" , time() + 2 * 24 * 60 * 60); ?> <html> <body> <?php echo "Auction Item is a " . $_COOKIE [ "Auction_Item" ]; ?> <p> <strong>Note:</strong> You might have to reload the page to see the value of the cookie. </p> </body> </html> |
输出:
![图片[3]-PHP Cookies-yiteyi-C++库](https://media.geeksforgeeks.org/wp-content/uploads/20211128221013/2.png)
访问Cookie值
删除Cookies :setcookie()函数可用于删除cookie。要删除cookie,可以通过传递cookie名称和其他参数或空字符串来调用setcookie()函数,但这一次,需要将过期日期设置为过去。要删除名为“Auction_Item”的cookie,可以执行以下代码。
例子: 本例描述了cookie值的删除。
PHP
<!DOCTYPE html> <?php setcookie( "Auction_Item" , "Luxury Car" , time() + 2 * 24 * 60 * 60); ?> <html> <body> <?php setcookie( "Auction_Item" , "" , time() - 60); ?> <?php echo "cookie is deleted" ?> <p> <strong>Note:</strong> You might have to reload the page to see the value of the cookie. </p> </body> </html> |
输出:
![图片[4]-PHP Cookies-yiteyi-C++库](https://media.geeksforgeeks.org/wp-content/uploads/20211128222124/3.png)
删除Cookie
要点:
- 如果cookie的过期时间设置为0或省略,则cookie将在会话结束时(即浏览器关闭时)过期。
- 应该传递用于创建cookie的相同路径、域和其他参数,以确保删除正确的cookie。
PHP是一种专门为web开发设计的服务器端脚本语言。通过以下步骤,您可以从头开始学习PHP PHP的教程 和 PHP示例 .