饼干插件登陆怎么用_用PHP烤饼干

本文介绍了HTTP协议中的cookies,用于维持用户状态的关键技术。详细讲解了PHP中设置、检索和更新cookies的过程,以及cookies的生命周期。文章还强调了在使用cookies时应注意的事项,包括浏览器对cookies数量和大小的限制,以及用户的隐私设置。最后,提醒开发者在用户禁用cookies时考虑其他保持状态的方法,如PHP会话。
摘要由CSDN通过智能技术生成

饼干插件登陆怎么用

Have you ever wondered that in spite of HTTP being a stateless protocol, when you log in to a website and buy stuff and checkout how the server can identify you uniquely? You might wonder if HTTP is stateless but your state is maintained through your interactions, isn’t this a contradiction? Welcome to world of cookies (not the ones which we can eat, btw :)), one the of primary ways to maintain user state and interaction between the web browser and the web server.

您是否曾经想过,尽管HTTP是无状态协议,但是当您登录网站并购买商品并检查服务器如何唯一地标识您时,您是否会感到好奇? 您可能想知道HTTP是否是无状态的,但是您的状态是通过交互来维护的,这不是矛盾吗? 欢迎来到cookie的世界(不是我们可以吃的cookie,顺便说一句:)),它是维护用户状态以及Web浏览器和Web服务器之间交互的主要方式之一。

Cookies are tid-bits of information stored by the browser on a user’s computer. The information stored in the cookie is used to uniquely identify a user and this information is sent to server with each request so server can make use of it. Cookies can store a variety of data, such as your name, the date of your last visit, shopping cart contents, etc. Cookies stored by one website can not be accessed by other websites, which makes cookies relatively safe to store personal information. Still, it is a good idea not to store sensitive information in them, like passwords and credit card information.

Cookies是浏览器存储在用户计算机上的信息的小知识。 Cookie中存储的信息用于唯一标识用户,并且此信息随每个请求发送到服务器,以便服务器可以使用它。 Cookies可以存储各种数据,例如您的姓名,您的上次访问日期,购物车内容等。一个网站存储的Cookies无法被其他网站访问,这使得Cookies相对安全地存储了个人信息。 不过,最好不要在其中存储敏感信息,例如密码和信用卡信息。

Cookie的生命周期 (The Lifecycle of a Cookie)

Here’s what the lifecycle of a PHP cookie looks like, from baking to eating:

这是从烘烤到食用PHP cookie的生命周期:

alt

There are no cookies when the browser connects to particular server for first time. When the request is made to the PHP script, the script makes a call to the setcookie() function. This causes a Set-Cookie HTTP header to be sent in the response that contains the name and value of the cookie to be set.

当浏览器首次连接到特定服务器时,没有cookie。 当向PHP脚本发出请求时,该脚本将调用setcookie()函数。 这将导致在响应中发送Set-Cookie HTTP标头,其中包含要设置的cookie的名称和值。

When the browser receives the response, it stores the value of the Set-Cookie header as a cookie locally. When multiple requests are made to server afterwards, the browser includes a Cookie header containing the name and value of the cookie. PHP intercepts it and creates an entry in the $_COOKIE array with name and value of the cookie.

当浏览器收到响应时,它将Set-Cookie标头的值存储为本地cookie。 之后,如果向服务器发出多个请求,则浏览器将包含一个Cookie标头,其中包含Cookie的名称和值。 PHP会拦截它,并在$_COOKIE数组中创建一个条目,其中包含cookie的名称和值。

烘烤PHP Cookies (Baking PHP Cookies)

PHP provides access to cookies through a function named setcookie() and the superglobal array $_COOKIE. setcookie() stores data in cookies, and $_COOKIE retrieves values from cookies.

PHP通过名为setcookie()和超全局数组$_COOKIE的函数提供对cookie的访问。 setcookie()将数据存储在cookie中,而$_COOKIE从cookie中检索值。

设置饼干 (Setting Cookies)

The function setcookie() is used to set a value and the optional expiration date of a cookie. The syntax for the function is:

函数setcookie()用于设置cookie的值和可选的到期日期。 该函数的语法为:

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

The meaning of each parameter and whether it is required or optional is listed in the following table adapted from one appearing on W3Schools:

下表列出了每个参数的含义以及它是必需参数还是可选参数,这些参数改编自W3Schools上显示的参数:

alt

Let’s look at an example of setting a cookie in PHP code.

让我们看一个在PHP代码中设置cookie的示例。

<?php
$firstcookie = "my first cookie";
$expiry = time() + (60 * 60 * 8);

// send a cookie that expires in 8 hours
setcookie("FirstCookie", $firstcookie, $expiry);

The code sets the cookie value in the variable $firstcookie and the expiration date in variable the $expiry. The cookie name is set as “FirstCookie” in the call to the function setcookie(). The cookie name can be anything you wish.

代码设置变量中的cookie值$firstcookie和可变到期日的$expiry 。 在对函数setcookie()的调用中,cookie名称设置为“ FirstCookie”。 Cookie名称可以是您想要的任何名称。

Note the cookie will expire in 8 hours (seconds × minutes × hours beyond the current time). But what if you want your cookie to be deleted immediately or once its information is retrieved by the browser? You can set the expiration date to a time in the past. For example, you can set $expiry as time()-3600.

请注意,该Cookie将在8小时后过期(秒数×分钟×小时,超过当前时间)。 但是,如果您希望立即删除Cookie,或者一旦浏览器检索到Cookie的信息,该怎么办? 您可以将到期日期设置为过去的时间。 例如,您可以将$expiry设置为time()-3600

Cookies by default are set only for the current directory and its descendants. The fourth parameter path restricts access to the cookies to a given path on your server. For example, if the cookie is set with “/test/” directory, then it will be available only to scripts in the test directory and its subdirectories. If you want cookie to be set for root directory, then “/” should be used as path parameter, as in this example:

默认情况下,仅为当前目录及其后代设置Cookie。 第四个参数路径将对cookie的访问限制为服务器上的给定路径。 例如,如果将cookie设置为“ / test /”目录,则它仅对test目录及其子目录中的脚本可用。 如果要为根目录设置cookie,则应使用“ /”作为路径参数,如以下示例所示:

<?php
setcookie("FirstCookie", $firstcookie, $expiry, "/");

The fifth parameter domain restricts access to the cookie to a given domain. For example, if you want a cookie to be accessed from two different web servers like www.trial.com and support.trial.com then set the domain parameter as .trial.com. Doing this will make cookie available to both servers.

第五个参数将对cookie的访问限制为给定的域。 例如,如果要从两个不同的Web服务器(如www.trial.com和support.trial.com)访问cookie,则将domain参数设置为.trial.com。 这样做将使cookie可用于两个服务器。

<?php
setcookie("FirstCookie", $firstcookie, $expiry, "/", ".trial.com");

Cookies are sent to the browser using header fields in the HTTP protocol. Because of this, it’s necessary to set cookies before sending a single line of HTML or any other output to user. Cookies will not be set if any output is sent. In this case, the setcookie() function will return false and PHP will produce an error message.

Cookies使用HTTP协议中的标头字段发送到浏览器。 因此, 有必要在向用户发送一行HTML或任何其他输出之前设置cookie。 如果发送任何输出,则不会设置Cookies。 在这种情况下, setcookie()函数将返回false,PHP将产生一条错误消息。

检索和更新Cookie (Retrieving and Updating Cookies)

Retrieving cookies is fairly simple in PHP. The global array $_COOKIE is used to retrieve the cookie value for subsequent page requests. For example, if you want to display the number of times a user has visited, then the following code should do the trick:

在PHP中,检索cookie非常简单。 全局数组$_COOKIE用于检索后续页面请求的cookie值。 例如,如果要显示用户访问的次数,则以下代码应该可以解决问题:

<?php
$visits = 1;
if (isset($_COOKIE["visits"])) {
    $visits = (int)$_COOKIE["visits"];
}
setcookie("visits", $visits + 1, time() + (60 * 60 * 24 * 30));
echo "You have visited this page $visits time(s).";

A cookie is automatically deleted by web browser once its expiration date passes. So, setting the expiration parameter of setcookie() function to some arbitrary time in the past deletes the cookie. setcookie() uses same domainname, pathname, and cookiename as specified when the cookie was created; only the value and expire parameter has to change. Here the value parameter is set to null and the expire parameter is set to some arbitrary time in past in this example.

一旦过期,cookie就会被网络浏览器自动删除。 因此,将setcookie()函数的expiration参数设置为过去的任意时间会删除cookie。 setcookie()使用与创建cookie时指定的域名,路径名和cookiename相同的名称; 只需更改value和expire参数。 在此示例中,此处的value参数设置为null,expire参数设置为过去的任意时间。

<?php
$expiry = time() - 60;
setcookie("FirstCookie", $firstcookie, $expiry, "/", ".trial.com");

最后的面包屑 (Final Crumbs)

There are cases when a user may wish to turn off cookies in the browser for privacy reasons. Therefore, before using cookies, it is recommended to always first test whether the user has cookies enabled or not in browser. You can do this by setting a cookie then redirecting to next page with flag in URL and checking if the cookie was received back. If not, then display a message to user suggesting they enable cookies.

在某些情况下,出于隐私原因,用户可能希望关闭浏览器中的cookie。 因此,在使用cookie之前,建议始终先测试用户是否在浏览器中启用了cookie。 您可以通过设置Cookie,然后重定向到带有URL中标记的下一页并检查是否收到了Cookie来完成此操作。 如果不是,则向用户显示一条消息,提示他们启用cookie。

Disabling cookies on a site that requires cookies thus disables the site’s functionality. In this case, we need to find other ways to maintain state. One alternative is to use PHP sessions and append a sessionID to the URL, but beware this approach can lead to social attacks.

因此,在需要cookie的站点上禁用cookie会禁用站点的功能。 在这种情况下,我们需要找到其他维护状态的方法。 一种替代方法是使用PHP会话并将sessionID附加到URL,但是请注意,这种方法可能导致社交攻击。

When using cookies, there are a few things you should keep in mind:

使用Cookie时,请注意以下几点:

  • A server can define multiple cookies with different names, but browsers limit the number of cookies per server (the count varies between browsers, but is generally around 20).

    一个服务器可以定义多个具有不同名称的cookie,但是浏览器会限制每个服务器的cookie数量(浏览器之间的数量有所不同,但通常为20个左右)。
  • The maximum size of any cookie is 4KB.

    任何cookie的最大大小为4KB。
  • Although you set an expiration on the cookie, a user can delete cookies at any time.

    尽管您在cookie上设置了过期时间,但是用户可以随时删除cookie。
  • Cookies can only be accessed by the browser that set them (Firefox and IE don’t share them).

    Cookies只能由设置了它们的浏览器访问(Firefox和IE不共享它们)。
  • A user can turn cookies off in their browser.

    用户可以在其浏览器中关闭Cookie。
  • Cookies must be set before any other output is sent from the PHP script or else you will receive an error.

    必须先设置Cookies,然后才能从PHP脚本发送任何其他输出,否则您将收到错误消息。

That’s all for cookies. You should now be able to use cookies in your PHP applications, so start baking and let me know how your cookies taste!

Cookie就这些了。 您现在应该可以在PHP应用程序中使用cookie,因此开始烘焙,让我知道您cookie的味道!

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/baking-cookies-in-php/

饼干插件登陆怎么用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值