【发布时间】:2021-07-27 01:41:28
【问题描述】:
如果用户没有在网站上进行任何类型的活动,我想销毁会话。 当时5个用户在索引页面上自动重定向。这怎么可能? 可以在 php 中进行会话处理,为此我必须维护或更新用户登录时间..
【问题讨论】:
标签: php
如果用户没有在网站上进行任何类型的活动,我想销毁会话。 当时5个用户在索引页面上自动重定向。这怎么可能? 可以在 php 中进行会话处理,为此我必须维护或更新用户登录时间..
【问题讨论】:
标签: php
在这里使用这个小 sn-p 相对容易实现:
if(time() - $_SESSION['timestamp'] > 900) { //subtract new timestamp from the old one
echo"<script>alert('15 Minutes over!');</script>";
unset($_SESSION['username'], $_SESSION['password'], $_SESSION['timestamp']);
$_SESSION['logged_in'] = false;
header("Location: " . index.php); //redirect to index.php
exit;
} else {
$_SESSION['timestamp'] = time(); //set new timestamp
}
【讨论】:
$_SESSION['timestamp'] 的初始值为 null (0),因此您最终将无限循环返回 login.php,就好像语句始终为真(大于 900)
我从 Sitepoint.com 获得了这个解决方案 在 html 中使用简单的元标记
<meta http-equiv="refresh" content="900;url=logout.php" />
900 是您希望会话在不活动时终止的时间(以秒为单位)。
希望对你有用
编辑:此方法不实现任何其他逻辑,因此只有在您想“强制”注销时才有效,如 cmets 中所述
【讨论】:
include 'header.php'
您可以为特定时间创建 cookie。 例如,您可以将其放在您的登录页面上:
<?php
setcookie('admin', 'abc', time()+50);
?>
然后在每个页面中包含的某些文件部分中,例如“header.php”,您可以包含:
<?php
if (!isset($_COOKIE['admin'])) {
echo "<script> location.href='logout.php'; </script>";
}
setcookie('admin', 'abc', time()+50);
?>
在上面的例子中,cookie 会在 50 秒后失效,用户会自动退出。
【讨论】:
我的解决方案是 (我给你解决方案,但这种简单的语法没有被尝试过)
checkerOrCreatorTime.php
<?php
//if using the session, this additional advice me
ini_set('session.cookie_httponly', 1);
ini_set('session.use_only_cookies', 1);
session_start();
//create session (JUST FOR ONE TIME)
if (!isset($_SESSION['THE SESSION KEY FOR LOGIN (EX. USERNAME)'])){
//create anyting session you need
$_SESSION['user']['THE SESSION KEY FOR LOGIN (EX. USERNAME)'] = 'USER';
$_SESSION['user']['TIME'] = '900';
}else
if (time() -$_SESSION['TIME'] > 900){
unset($_SESSION['user']);
// and whatever your decision
}
?>
常见问题:
1. Why use ['user'] is session login?
if you using many session for user, you just unset one var, like this.
2. why use a ini_set.... in this syntax?
for more security
如果您喜欢使用现代网络,只需将 javascript 用于 ajax
【讨论】:
$_SESSION['user']['TIME'] 这将为每个用户设置一个单独的变量,但是 $_SESSION 变量已经为每个用户定义了一次,因此您不需要这样做。只需执行 $_SESSION['TIME'] 即可。
<form action="index.php" method="post" name="frm"><input name="uname" type="text" placeholder="User Name" />
<input name="pass" type="password" placeholder="Password" />
<input name="submit" type="submit" value="submit" /></form>
In index.php
<?php if(isset($_SESSION['loggedAt'])) { header('dashboard.php'); }
if(isset($_POST['submit'])) { $name=$_POST['uname']; $pass=$_POST['pass'];
if($name=="admin" &amp;amp;&amp;amp; $pass=="1234") {
session_Start(); $_SESSION['username']=$name; $_SESSION['loggedAt']=time(); header('location:dashboard.php?msg=Welcome to dashboard'); } } ?>
in dashboard.php
if(time() - $_SESSION['loggedAt'] > 240) {
echo"<script>alert('Your are logged out');</script>";
unset($_SESSION['username'], $_SESSION['loggedAt']);
header("Location: " . index.php);
exit;
} else {
$_SESSION['loggedAt'] = time();
}
【讨论】:
此代码包含在connection.php中以确保该代码包含在任何页面中,但您可以在任何您想要的页面上实现
if (isset($_SESSION['user-session']) OR isset($_SESSION['admin-session']) ) {
//then we are checking the activity sesssion $_SESSION['']
if (isset($_SESSION['last_active'])) {
//if the time is set then we check the difference
$max_time=5*60; #number of seconds
$now=microtime(date("H:i:s"));
//Checking the last active and now difference in seconds
$diff=round(microtime(date("H:i:s"))- $_SESSION['last_active']); #the difference of time
if ($diff>=$max_time) { #if the difference is greater than the allowed time!
//echo "logging out couse the time is".$diff;
header("location:logout.php");
}else {
$time=microtime(date("H:i:s"));
$_SESSION['last_active']=$time; #Updating the time
//echo 'More time added the time was!'.$diff;
}
}else{
//if there is no last active then we create it over here
$time=microtime(date("H:i:s"));
$_SESSION['last_active']=$time;
}}
【讨论】:
使用 .htaccess 的简单解决方案
将以下行添加到您的 .htaccess 文件中,其中 3600 是秒数。 会话将在一定时间后自动销毁,与活动或不活动无关。
根据以下代码会话将在 1 小时后销毁。
php_value session.gc_maxlifetime 3600
php_value session.gc_probability 1
php_value session.gc_divisor 1
【讨论】:
这是代码示例。
session_start();
$t=time();
if (isset($_SESSION['logged']) && ($t - $_SESSION['logged'] > 900)) {
session_destroy();
session_unset();
header('location: index.php');
}else {
$_SESSION['logged'] = time();
}
【讨论】: