I have developed a class that utilises the session_set_saver_handler
function so I can store sessions within my DB. The class works just as I would like.
However, my only concern is the way I have approached the session timeout.
Current the _read()
function code looks like:
/**
* Read session function
* @access public
* @return the 'data' record providinf the PDO statement executed correctly. Otherwise, return false.
*/
public function _read($id)
{
$timeout = time() - $this->accessTime;
$locked = false;
$this->database->query('SELECT updatedTime, data FROM sessions WHERE session = :id AND locked = :locked');
$this->database->bind(':id', $id);
$this->database->bind(':locked', $locked);
if($this->database->execute())
{
if($this->database->rowCount() > 0)
{
$row = $this->database->singleResult();
if($row['updatedTime'] < $timeout)
{
//Set the location of the user.
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($url != $this->redirectUrl)
{
header('Location: ' . $this->redirectUrl);
return;
}
return '';
}
return $row['data'];
}
}
return '';
}
When I originally create the script I hard coded the redirect URL. The problem was logout.php
(the file the user is redirected to) contains the session class. Meaning I have a constant loop. So I approached it by implementing the following:
if($row['updatedTime'] < $timeout)
{
//Set the location of the user.
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($url != $this->redirectUrl)
{
header('Location: ' . $this->redirectUrl);
return;
}
return '';
}
This seems to me more of a 'hack' than intelligent code.
Did I approach this correct (probably not)? If not, what would be a more intelligent work around?