美文网首页
PHP中seesion的冷知识(session_set_cook

PHP中seesion的冷知识(session_set_cook

作者: Zeaone | 来源:发表于2019-03-12 19:44 被阅读0次

设置一个cookie的过期时间通常有两种方式
1.session_set_cookie_params()
2.setCookies()
那么这两种方式到底有什么区别呢?看php手册里的解释

As PHP's Session Control does not handle session lifetimes correctly when using session_set_cookie_params(), we need to do something in order to change the session expiry time every time the user visits our site. So, here's the problem.
//由于PHP的会话控制通常在使用session_set_cookie_params()时不处理会话生存时间,为了使我们的网站可以在每次访问时改变session的过期时间,我们需要做些什么,这就是问题所在

<?php
  $lifetime=600;
  session_set_cookie_params($lifetime);
  session_start();
?>

This code doesn't change the lifetime of the session when the user gets back at our site or refreshes the page. The session WILL expire after $lifetime seconds, no matter how many times the user requests the page. So we just overwrite the session cookie as follows:
//当用户返回我们的网站或者刷新当前页面时,这个代码并不会改变session 的生存时间,session依然会在$lifetime时长之后过期,不管我们请求多少次页面。因此我们知识会覆盖会话,如下所示

<?php
  $lifetime=600;
  session_start();
  setcookie(session_name(),session_id(),time()+$lifetime);
?>

And now we have the same session cookie with the lifetime set to the proper value.
//现在我们有相同的会话cookie,其生命周期被设置为正确的值。

总结:
1.session_set_cookie_params() 不管刷不刷新页面都不会改变cookie的过期时间
2.setCookies() 每刷新一次就会重置一次过期时间

相关文章

网友评论

      本文标题:PHP中seesion的冷知识(session_set_cook

      本文链接:https://www.haomeiwen.com/subject/jwqopqtx.html