php cookies_用PHP编写Cookies

php cookies

Creating cookies via couldn’t be simpler: anywhere in the document (normally before anything else is written to the page), you can create a cookie with the setCookie() function:

通过创建cookie并非如此简单:在文档中的任何位置(通常在将其他内容写入页面之前),都可以使用setCookie()函数创建cookie:

<?php setCookie("name","value"); ?>

The name of the cookie can be anything that follows web conventions, and the value can be a string, Boolean value, or number.

Cookie的名称可以是遵循Web约定的任何名称,其值可以是字符串,布尔值或数字。

Once it is set, you read a cookie from any page as a variable (keeping in mind that you cannot set a cookie and read it in the same session). So creating a cookie with:

设置后,您可以从任何页面读取cookie作为变量(请记住,您无法设置cookie并在同一会话中读取它)。 因此,使用以下命令创建一个cookie:

<?php setCookie("cookietest", "test"); ?>

Could be displayed at a later point with:

可以在以后显示:

<?php echo $_COOKIE['cookietest']; ?>

This cookie will last as long as the browser remains running: you can close the window or tab that contains your web page and re-open it, and the cookie will still be there. However, the moment you quit the browser, the cookie will be discarded. To make the cookie permanent, you need to give it an expiry date.

只要浏览器保持运行,该cookie就会一直存在:您可以关闭包含您的网页的窗口或选项卡,然后重新打开它,并且该cookie仍然在那里。 但是,退出浏览器后,该cookie将被丢弃。 要使Cookie永久保存,您需要给它指定一个有效期。

时间() (time())

time() is a fascinating function. The easiest way to understand it is to simply echo it on a PHP page:

time()是一个令人着迷的功能。 理解它的最简单方法是在PHP页面上简单地回显它:

<?php echo time(); ?>

time() is a constant record of the number of seconds that have passed since midnight of January 1, 1970, Greenwich Mean Time, aka the “Unix Epoch”. Most Unix-based operating and file systems (i.e. the majority of the Internet) do not think of time in terms of BC or AD, but as the number of seconds that have elapsed from this point.

time()是不断记录从1970年1月1日午夜格林威治标准时间(又称“ Unix时代”)以来经过的秒数。 大多数基于Unix的操作系统和文件系统(即Internet的大部分)都不以BC或AD来考虑时间,而是以从这一点起经过的秒数来考虑。

The current moment always represented by time(). 30 seconds into the past would be time() – 30; one minute into the future would be time() + 60, and one hour would be time() + (60 × 60) (sixty seconds multiplied by sixty minutes = the number of seconds in an hour). To give our cookie an expiry date 30 days from now, we would create it with:

当前时刻始终由time()表示。 过去30秒将是time() – 30 ; 未来的一分钟为time() + 60 ,一小时为time() + (60 × 60) (六十秒乘以六十分钟=一小时中的秒数)。 为了给我们的cookie设置一个30天的有效期,我们可以使用以下方法创建它:

<?php setCookie("cookietest", "test", time() + (60*60*24*30)); ?>

Now the cookie will last even if the browser or computer is shut down, until 30 days have elapsed since the cookie was set.

现在,即使关闭浏览器或计算机,cookie仍将持续,直到设置cookie到30天为止。

Note that this does not guarantee that the cookie will be present the next time the user returns: the cookie could be wiped or blocked, the hard drive reformatted, etc. You can see, and manipulate, the cookies set inside your browser: in Firefox, you will find this under Firefox / Preferences / Privacy / Remove Individual Cookies

请注意,这并不保证该cookie会在用户下次返回时出现:该cookie可能会被擦除或阻止,重新格式化硬盘等。您可以在Firefox中查看和操作浏览器中设置的cookie: ,您可以在Firefox /首选项/隐私/删除单个Cookie下找到此文件

边注 (Side Note)

There is also an interesting problem with time() in that it is traditionally programmed as a 32-bit number: this number will eventually run out of digits, an issue known as the “Year 2038 problem”, analogous to the Y2K bug.

time()还有一个有趣的问题,因为它通常被编程为32位数字:该数字最终将用完数字,这就是所谓的“ 2038年问题 ”,类似于Y2K错误。

翻译自: https://thenewcode.com/208/Writing-Cookies-With-PHP

php cookies

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值