使用自动更新计数器保持 $_SESSION 存活 [英] Keep $_SESSION alive with autorenewing counter

查看:48
本文介绍了使用自动更新计数器保持 $_SESSION 存活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让会话保持三天,每当他们访问页面时,它都会再次更新三天.基本上,如果他们三天不访问该网站,他们的访问量就会下降.

I'd like to keep sessions alive for three days, and whenever they visit the page it is renewed up to three days again. Basically if they don't visit the site for three days, their session drops.

ini_set('session.cookie_lifetime',60*60*24*3); //saw on a similar SO question
session_start();

上面这段代码在 top.php 的顶部,这个文件在每个页面上都被include().

The above piece of code is at the top of top.php, a file that is include()ed on every single page.

$_SESSION["username"] = $username;
$_SESSION["password"] = $hashedpass;
$_SESSION["authtoken"] = $authkey; //authentication key

上面是login.php,它根据SQL中存在的验证设置$_SESSION数据.

The above is on login.php which sets the $_SESSION data according to verification it exists in SQL.

也许这是 WAMP 的一个功能,但是每当我关闭浏览器时,尽管 php.ini 参数被更改,会话也会终止.我可能在 top.php 的第 1 行使用了错误的位吗?

Maybe this is a function of WAMP, but whenever I close the browser, the session terminates despite the php.ini params being changed. Am I perhaps using the wrong bit on line 1 of top.php?

推荐答案

使用 cookies,我怀疑你能控制你的会话寿命.

Use cookies, I doubt you can control your session lifetime.

您需要做的基本上是将所需的会话变量存储在 cookie 中,当用户进入站点时,如果未设置会话变量,请检查是否存在带有这些变量的 cookie,并将 cookie 中的数据加载到会话中.或者您可以从 cookie 中访问它们

What you need to do is basically store session variables needed in cookies, and upon a user entering the site, if the session variables are not set, check if there exist cookies with those variables and load the data from the cookie into session. Or you can just access them from the cookies

如果您有不想存储在 cookie 中的敏感数据.您可以将这些数据以某种形式存储在您的服务器上(数据库或文件,最好是数据库),并将这些数据的 ID 存储在用户端的 cookie 中.当用户访问您的网站时,从 cookie 中获取 ID,并加载数据.

In the case you have sensitive data that you don't want to store in cookies. You can store this data in some form of persistent storage on your server (Database or File, Database is preferable) and store the ID of this data in a cookie on the user side. When the user accesses your site, grab the ID from the cookie, and load the data.

这篇关于使用自动更新计数器保持 $_SESSION 存活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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