创建持久的PHP登录Cookie会话 [英] Create lasting php login cookie sessions

查看:143
本文介绍了创建持久的PHP登录Cookie会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的登入工作阶段持续时间更长,让使用者不会太早登出我的网站。例如,撰写博客文章并在他们提交时失去它,因为PHP已过期了他们的Cookie。

I'm trying to make my login sessions last longer, so that people don't get logged out of my website too early. For example, making a blog post and losing it when they submit because php expired their cookie.

理想情况下,我想给他们说一个2小时的会话,不会被注销,每次加载页面时都会刷新(以下代码段位于每个安全页面的标题之前)

Ideally I'd like to give them say a 2 hour session where they won't be logged out, which will refresh every time they load the page (this code snippet below is before the header of each secure page)

这是我想要的,但它出来一个错误的setcookie()说有一个除以零?

This is what I am trying, but it comes up with an error for the setcookie() saying that there was a division by zero? What am I doing wrong here?

//How long sessions last
$hours = 2;

// php.ini setting required for session timeout.
ini_set('session.gc_maxlifetime',$hours*60*60);
ini_set('session.gc_probability',1);
ini_set('session.gc_divisor',1);

//Set the session parameters and start session
$sessionCookieExpireTime=$hours*60*60;
session_set_cookie_params($sessionCookieExpireTime);
session_start();

// Reset the expiration time upon page load
if (isset($_COOKIE[session_name()]))
{
    setcookie(session_name(), $_COOKIE[session_name()], time() + $sessionCookieExpireTime, "/");
}

编辑:现在工作的问题是非标准报价撇号。只是为了防止任何人复制此代码并使用它。

推荐答案

如果您希望在Cookie过期时添加以下内容: / p>

If you would like you could add this for when your cookie expires:

time()+60*60*24*30

这就像说,cookie在60秒,60分钟,24小时过期等等。您还应该查看有关Cookie的教程: http://www.w3schools.com/php/ php_cookies.asp

This is like saying that the cookie expires in 60secs, 60mins, 24h and so on. You should also check out a tutorial on cookies here: http://www.w3schools.com/php/php_cookies.asp

这篇关于创建持久的PHP登录Cookie会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆