Link to home
Start Free TrialLog in
Avatar of shruti A
shruti A

asked on

Logout session for every 4 hour once

I need to logout for my website so  I'm trying logout automatically for every 4 hour once ,i'm not getting how to do  so  please let me know the solution
<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>

Open in new window



<?php
//on pageload
session_start();

$idletime=60;//after 60 seconds the user gets logged out

if (time()-$_SESSION['timestamp']>$idletime){
    session_destroy();
    session_unset();
}else{
    $_SESSION['timestamp']=time();
}

//on session creation
$_SESSION['timestamp']=time();

Open in new window




how to send session variable from html to php please help me to get solution .

Thank you
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Rather use a cookie.

Create the cookie with an expiry date 4 hours in the future. Send that cookie to the browser

When a user connects - check the cookie - if it exists - all is good - refresh the expiry to 4 hours in the future.

No cookie - then the session has expired.
setcookie()
<?php
// SECURE PAGE
if (isset($_COOKIE['YOURSESSIONNAME'])) {
   // Session is still active
   // REFRESH IT
   setcookie('YOURSESSIONNAME', 'value you want to store', time() + 14400);
}
else {
   // SESSION EXPIRED SO BOUNCE TO THE LOGIN PAGE
   header('location: login.php');
}

Open in new window

Login page
// CHANGE TO MATCH YOUR AUTHENTICATION PROCESS
if (Login($username, $password)) {
  setcookie('YOURSESSIONNAME', 'value you want to store', time() + 14400);
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.