php获取cookies_PHP Cookies:如何设置Cookies和获取Cookies

php获取cookies

Cookies don't have to be an essential part of a website but can provide some of the "little things" that can set your website apart from the rest. Cookies are small tidbits of information that you save on the client's computer so that you can access them next time they visit the website. Session ID's are also usually held in cookies.

Cookies不一定是网站的必要组成部分,但可以提供一些使网站与众不同的“小东西”。 Cookies是您保存在客户计算机上的一小部分信息,以便下次他们访问网站时可以访问它们。 会话ID通常也保存在cookie中。

So what are the most popular uses of cookies? They are:

那么,cookie最受欢迎的用途是什么? 他们是:

  • To store username/password information so that the user doesn't have to log in every time they visit the website ("remember me" sign ins).

    存储用户名/密码信息,以便用户不必在每次访问网站时都登录(“记住我”登录信息)。
  • To simply remember the user's name.

    简单地记住用户名。
  • To keep track of a user's progress during a specified process.

    在指定的过程中跟踪用户的进度。
  • To remember a user's theme.

    记住用户的主题。

设置Cookie (Setting the Cookie)

Setting a cookie requires a key, a value, and the amount of time to allow the cookie to exist.

设置cookie需要密钥,值和允许cookie存在的时间。

$first_name = 'David';
setcookie('first_name',$first_name,time() + (86400 * 7)); // 86400 = 1 day

Above, we set the user's first name equal to 'David' (this data would actually come from a form or database but for the sake of the example we'll use my name). Then, we set a cookie with the key of "first_name" with the value 'David', and program it to expire 7 days from now.

上面,我们将用户的名字设置为“ David” (此数据实际上来自表单或数据库,但出于示例的原因,我们将使用我的名字) 。 然后,我们使用键“ first_name”将Cookie设置为值“ David”,并将其编程为从现在起7天到期。

获取Cookie值 (Getting the Cookie Values)

Now that we've set our cookie, it's time to get the value pretend they left your site and are coming back two days later).

现在我们已经设置了cookie,现在该假装他们离开您的网站并在两天后回来的时候获得价值。

echo 'Hello '.($_COOKIE['first_name']!='' ? $_COOKIE['first_name'] : 'Guest'); // Hello David!

Above, we check to see if the cookie with 'first_name' as the key still exists. If so, we use their name; if not, we call them "Guest". Basic cookies are that easy!

在上方,我们检查以“ first_name”作为键的cookie是否仍然存在。 如果是这样,我们使用他们的名字。 如果没有,我们称他们为“客人”。 基本的cookie就是那么容易!

PHP cookies can be set with more specific directives, including path, domain, secure, and httponly.

可以使用更具体的指令设置PHP cookie,包括路径,域,安全和仅HTTP指令。

setcookie('first_name',$first_name,time() + (86400* 7),'/~sugar/','davidwalsh.name',true,true);

This cookie is the same as above, but we're also telling the cookie to be applied towards the "~sugar" directory on the "davidwalsh.name" domain. It is for use only on an SSL connection and it may not be used by JavaScript.

该cookie与上面的cookie相同,但是我们还告诉该cookie将应用于“ davidwalsh.name”域上的“〜sugar”目录。 它仅用于SSL连接,JavaScript可能不使用。

Some other things to know about cookies:

有关Cookie的其他一些注意事项:

  • 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)

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

    用户可以在其浏览器中关闭Cookie。
  • Never assume a cookie exists.

    永远不要假设存在cookie。

翻译自: https://davidwalsh.name/php-cookies

php获取cookies

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值