在php中无效15分钟后自动注销

时间:2021-10-09 05:27:14

I want to destroy session if users are not doing any kind of activity on website. At that time after 5 users automatically redirect on index page. How is it possible? Is possible in php with session handling and for that I have to maintain or update user login time or not..

如果用户没有在网站上进行任何类型的活动,我想破坏会话。此时5个用户自动重定向索引页面。这怎么可能?可以在php中进行会话处理,为此我必须维护或更新用户登录时间。

5 个解决方案

#1


46  

Pretty easy:

 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
}

#2


4  

I got this solution from Sitepoint.com Using a simple meta tag in your html

我从Sitepoint.com获得了这个解决方案在你的html中使用了一个简单的元标记

<meta http-equiv="refresh" content="900;url=logout.php" />

The 900 is the time in seconds that you want the session to be terminated if inactive.

900是您希望会话在非活动状态下终止的时间(以秒为单位)。

Hope it works for you

希望对你有帮助

#3


0  

My Solution Is (i give you solution but this simple and syntax not been tried)

我的解决方案是(我给你解决方案,但这个简单和语法没有尝试过)

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
}
?>

Faq:

 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

if you like using modern web, just using javascript for ajax

如果你喜欢使用现代网络,只需使用javascript进行ajax

#4


0  

                      session_start();
                      $t=time();
                      if (isset($_SESSION['logged']) && ($t - $_SESSION['logged'] > 900)) {
                      session_destroy();
                      session_unset();
                      header('location: index.php');
                      }else {$_SESSION['logged'] = time();}                          

#5


0  

<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;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();
}
For details
Follow the link

http://datainflow.com/auto-logout-session-php/

#1


46  

Pretty easy:

 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
}

#2


4  

I got this solution from Sitepoint.com Using a simple meta tag in your html

我从Sitepoint.com获得了这个解决方案在你的html中使用了一个简单的元标记

<meta http-equiv="refresh" content="900;url=logout.php" />

The 900 is the time in seconds that you want the session to be terminated if inactive.

900是您希望会话在非活动状态下终止的时间(以秒为单位)。

Hope it works for you

希望对你有帮助

#3


0  

My Solution Is (i give you solution but this simple and syntax not been tried)

我的解决方案是(我给你解决方案,但这个简单和语法没有尝试过)

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
}
?>

Faq:

 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

if you like using modern web, just using javascript for ajax

如果你喜欢使用现代网络,只需使用javascript进行ajax

#4


0  

                      session_start();
                      $t=time();
                      if (isset($_SESSION['logged']) && ($t - $_SESSION['logged'] > 900)) {
                      session_destroy();
                      session_unset();
                      header('location: index.php');
                      }else {$_SESSION['logged'] = time();}                          

#5


0  

<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;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();
}
For details
Follow the link

http://datainflow.com/auto-logout-session-php/