php - using $_session and session_start() with int column

789

I want to check how much time has elapsed since the session started and compare with my database column value in INT to see if it's expired and logout the user. in the place of inactive= 10; in my code below i want to pass the db column value either in INT

<?php require_once('Connections/noundb.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}
 $inactive = 10; // Set timeout period in seconds

if (isset($_SESSION['timeout'])) {
    $session_life = time() - $_SESSION['timeout'];
    if ($session_life > $inactive) {
        session_destroy();
        header("Location: l.php");
    }
}
$_SESSION['timeout'] = time();



// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "l.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
935

Answer

Solution:

I suggest you to do session start before require once, and don't close and open php tags: this mean an output to send to the client,then no "managed" session can be started.

Try this:

<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

require_once('Connections/noundb.php');
// [other code ]

People are also looking for solutions to the problem: php - How to get the user's system clock time to display in a web application?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.