php 如何获取cookie的过期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4203225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:17:07  来源:igfitidea点击:

How to get cookie's expire time

phpcookies

提问by love

When I create a cookie, how to get cookie's expire time?

创建 cookie 时,如何获取 cookie 的过期时间?

采纳答案by Thariama

This is difficult to achieve, but the cookie expiration date can be set in another cookie. This cookie can then be read later to get the expiration date. Maybe there is a better way, but this is one of the methods to solve your problem.

这很难实现,但是可以在另一个 cookie 中设置 cookie 的到期日期。然后可以稍后读取此 cookie 以获取到期日期。也许有更好的方法,但这是解决问题的方法之一。

回答by Braikar

Putting an encoded json inside the cookie is my favorite method, to get properly formated data out of a cookie. Try that:

将编码的 json 放入 cookie 是我最喜欢的方法,从 cookie 中获取格式正确的数据。试试看:

$expiry = time() + 12345;
$data = (object) array( "value1" => "just for fun", "value2" => "i'll save whatever I want here" );
$cookieData = (object) array( "data" => $data, "expiry" => $expiry );
setcookie( "cookiename", json_encode( $cookieData ), $expiry );

then when you get your cookie next time:

那么当你下次拿到你的 cookie 时:

$cookie = json_decode( $_COOKIE[ "cookiename" ] );

you can simply extract the expiry time, which was inserted as data inside the cookie itself..

您可以简单地提取到期时间,该时间作为数据插入 cookie 本身..

$expiry = $cookie->expiry;

and additionally the data which will come out as a usable object :)

以及将作为可用对象出现的数据:)

$data = $cookie->data;
$value1 = $cookie->data->value1;

etc. I find that to be a much neater way to use cookies, because you can nest as many small objects within other objects as you wish!

等等。我发现这是使用 cookie 的一种更简洁的方式,因为您可以根据需要在其他对象中嵌套任意数量的小对象!

回答by K-Gun

You can set your cookie value containing expiry and get your expiry from cookie value.

您可以设置包含到期时间的 cookie 值,并从 cookie 值中获取到期时间。

// set
$expiry = time()+3600;
setcookie("mycookie", "mycookievalue|$expiry", $expiry);

// get
if (isset($_COOKIE["mycookie"])) {
  list($value, $expiry) = explode("|", $_COOKIE["mycookie"]);
}

// Remember, some two-way encryption would be more secure in this case. See: https://github.com/qeremy/Cryptee

// 请记住,在这种情况下,一些双向加密会更安全。见:https: //github.com/qeremy/Cryptee

回答by Hannes

When you create a cookie via PHP die Default Value is 0, from the manual:

当您通过 PHP 创建 cookie 时,默认值为 0,来自手册:

If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes)

如果设置为 0 或省略,cookie 将在会话结束时过期(当浏览器关闭时)

Otherwise you can set the cookies lifetime in seconds as the third parameter:

否则,您可以将 cookie 生存期(以秒为单位)设置为第三个参数:

http://www.php.net/manual/en/function.setcookie.php

http://www.php.net/manual/en/function.setcookie.php

But if you mean to get the remaining lifetime of an already existing cookie, i fear that, is not possible (at least not in a direct way).

但是如果你想获得一个已经存在的 cookie 的剩余生命周期,我担心这是不可能的(至少不是直接的)。

回答by Pedro Sousa

To get cookies expire time, use this simple method.

要获取 cookie 的过期时间,请使用这个简单的方法。

<?php

//#############PART 1#############
//expiration time (a*b*c*d) <- change D corresponding to number of days for cookie expiration
$time = time()+(60*60*24*365);
$timeMemo = (string)$time;

//sets cookie with expiration time defined above
setcookie("testCookie", "" . $timeMemo . "", $time);

//#############PART 2#############
//this function will convert seconds to days.
function secToDays($sec){

    return ($sec / 60 / 60 / 24);

}
//checks if cookie is set and prints out expiration time in days
if(isset($_COOKIE['testCookie'])){

    echo "Cookie is set<br />";
    if(round(secToDays((intval($_COOKIE['testCookie']) - time())),1) < 1){
        echo "Cookie will expire today.";
    }else{
        echo "Cookie will expire in " . round(secToDays((intval($_COOKIE['testCookie']) - time())),1) . " day(s)";
    }

}else{
    echo "not set...";
}

?>

?>

You need to keep Part 1 and Part 2 in different files, otherwise you will get the same expire date everytime.

您需要将Part 1 和 Part 2 保存在不同的文件中,否则每次都会得到相同的过期日期。

回答by sasho

It seems there's a list of all cookies sent to browser in array returned by php's headers_list()which among other data returns "Set-Cookie" elements as follows:

似乎有一个由 php 返回的数组发送到浏览器的所有 cookie 的列表,headers_list()其中包括返回“ Set-Cookie”元素的其他数据,如下所示:

Set-Cookie: cooke_name=cookie_value; expires=expiration_time; Max-Age=age; path=path; domain=domain

Set-Cookie: cooke_name=cookie_value; expires=expiration_time; Max-Age=age; path=path; domain=domain

This way you can also get deleted ones since their value is deleted:

这样你也可以得到删除的值,因为它们的值被删除了

Set-Cookie: cooke_name=deleted; expires=expiration_time; Max-Age=age; path=path; domain=domain

Set-Cookie: cooke_name=deleted; expires=expiration_time; Max-Age=age; path=path; domain=domain

From there on it's easy to retrieve expiration time or age for particular cookie. Keep in mind though that this array is probably available only AFTERactual call to setcookie()has been made so it's valid for script that has already finished it's job. I haven't tested this in some other way(s) since this worked just fine for me.

从那里可以轻松检索特定 cookie 的过期时间或年龄。但请记住,此数组可能是目前唯一AFTER实际通话setcookie()已取得所以它适用于该已经完成它的任务脚本。我没有以其他方式测试过这个,因为这对我来说很好用。

This is rather old topic and I'm not sure if this is valid for all php builds but I thought it might be helpfull.

这是一个相当古老的话题,我不确定这是否对所有 php 版本都有效,但我认为它可能会有所帮助。

For more info see:

有关更多信息,请参阅:

https://www.php.net/manual/en/function.headers-list.php
https://www.php.net/manual/en/function.headers-sent.php

https://www.php.net/manual/en/function.headers-list.php
https://www.php.net/manual/en/function.headers-sent.php