设为首页 收藏本站
查看: 556|回复: 0

[经验分享] [译文]Using Cookies with PHP

[复制链接]

尚未签到

发表于 2017-3-28 10:44:13 | 显示全部楼层 |阅读模式
  英:Cookies allow the webmaster to store information about the site visitor on their computer to be accessed again the next time they visit. One common use of cookies is to store your username and password on your computer so you don’t need to login again each time you visit a website. Cookies can also store other things such as your name, last visit, shopping cart contents, etc.

译:Cookies 使得网站管理员能够将站点来访者的信息储存在客户端机子上,以便下次来访可以直接获取。一个很常见的用法就是将用户名和密码储存在客户端机子上,使得用户不必每次来访都要重新登录。Cookies同时也能够存储其它信息如姓名、上次访问信息以及购物车里的内容,等等。

英:The main difference between a cookie and a session is that a cookie is stored on your computer, and a session is not. Although cookies have been around for many years and most people do have them enabled, there are some who do not. Cookies can also be removed by the user at any time, so don’t use them to store anything too important.

译:cookie和session之间最大的不同之处是cookie存储在客户端机子上,而session不是。尽管cookies已经存在很多年而且大多数的人们都是开启cookie,但是还是有一些人没有。用户可以在任意时刻删除掉本机的cookies,所以不应该用cookies来存储重要信息。

英:A cookie is set with the following code: setcookie(name, value, expiration)

译:cookie 是通过如下语法设置的: setcookie(name, value, expiration)

英:
<?php$Month = 2592000 + time();//this adds 30 days to the current timesetcookie(AboutVisit, date("F jS - g:i a"), $Month);?>

The above code sets a cookie in the visitor’s browser called “AboutVisit”. The cookie sets the value to the current date, and set’s the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)

译:如上代码在来访者的浏览器中设置了一个名为“AboutVisit”的cookie。该cookies以当前时间为值,并且设置30天后过期。

英:Now let’s retrieve the cookie.
<?phpif(isset($_COOKIE['AboutVisit'])){$last = $_COOKIE['AboutVisit'];echo "Welcome back! <br> You last visited on ". $last;}else{echo "Welcome to our site!";}?>

This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.

译:现在让我们来取回cookie。

这段代码首先检查该cookie是否存在。如果存在,它输出欢迎消息的同时,输出上次来访者的访问时间。如果cookie是新的,它会跳过 if 段的代码并且输出通用的欢迎消息。

英:

TIP: If you are calling a cookie(原文此处有误) on the same page you plan to set one - be sure you retrieve it first, before you overwrite it!

To destroy the cookie, simply use setcookie again, only set the expiration date to be in the past. This is often done when you ‘logout’ of a site. Here is an example:
<?php$past = time() - 10;//this makes the time 10 seconds agosetcookie(AboutVisit, date("F jS - g:i a"), $past);?>

REMEMBER:Cookies need to be set in the header. This means they must be sent before any HTML is set to the page, or they will not work.

译:

提示:如果要在同一个页面重新设置一个cookie,记得先取回该cookies再覆盖它。

如果要撤销一个cookie,只需很简单地再次利用setcookie,只要将expiration的值即过期值设置在过去的时间里。这在网站登出的功能中很常见。以下是示例。

请记住:cookies必须在header里头设置。这意味着cookies必须在HMTL之前发送,否则它们不会工作。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-356315-1-1.html 上篇帖子: php多文件上传` 下篇帖子: PHP创建与解析XML
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表