Re: Session problem



mabobine wrote:
if($session->userHasSession($user['id'])) {
throw new Exception('You can only have One session at a time. Please
wait for the other session to expire.');
}


public function userHasSession($user_id)
{
$this->cleanup();

// Check first, that the User exists on the Database
$query = 'select * from `users` where id = {user_id}';
$query_subst = array('user_id' => $user_id);
$mysqlres = mysqlConn::executeQuery($query, $query_subst);
if(mysql_num_rows($mysqlres) < 1) {
mysql_free_result($mysqlres);

// Block remaining operations, because the User is not valid on the
system
die("Exception: User requesting the Session is invalid!");

return false;
}

// Now, check if there is a Session for this User
$query = 'select * from `' . $this->table . '` where user_id =
{user_id}';
$mysqlres = mysqlConn::executeQuery($query, $query_subst);
if(mysql_num_rows($mysqlres) > 0) {
mysql_free_result($mysqlres);
return true;
} else {
mysql_free_result($mysqlres);
return false;
}
}

public function killSession()
{
if($this->started)
{
$query = 'DELETE FROM ' . $this->table . ' WHERE id =
{session_id}';
$query_subst = array('session_id' => $this->session_id);
if(mysqlConn::executeQuery($query, $query_subst))
{
setcookie('session_id', '', time() - 259200);

return true;
}
}

return false;
}








OK, you're keeping track of sessions in your MySQL database. You need to clean up the session, even if they just close their browser and never log off. That means removing the entry from your table, which you evidently are not doing (it's hard to tell since you only show part of your code).

The problem is - when to do it? You can do it on a timer (i.e. every 2 hours), for instance. Another way would be to delete the old session when they log on the new time.

This is a good reason why I don't uses a database to keep track of sessions. I just use the $_SESSION superglobal.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================

.



Relevant Pages

  • Re: PHP5 - Complete OO User/admin class
    ... public function login() { ... protected $session; ... echo "display admin menu..."; ... public function __construct( ...
    (php.general)
  • RE: [PHP] Storing objects in sessions recursively
    ... are you sure you did not forget a include of the Article source code? ... You need all sources codes aiavlable when getting the object back from the session. ... storing an array in an array. ... public function setCurrentArticle ...
    (php.general)
  • Storing objects in sessions recursively
    ... I have a class called "Session", which creates an object stored in the $_SESSION variable. ... As far as I can understand it, an object is basically just an array, and an object that "owns" an object as a member variable is just like storing an array in an array. ... public function setCurrentArticle ... public static function getSession() ...
    (php.general)
  • Re: [PHP] accessing one objects attribute from another
    ... and am working at improving my site by adding some classes etc. ... public function setThisVar{ ... There are a number of variables and methods that are common throughout ... stored in the object that is hanging around in the session. ...
    (php.general)
  • Re: Unhandled errors; public vars/classes lose scope??
    ... My custom class module's class_terminate event doesn't even fire, ... Public Session As clsSession ... Public Function SessionInit() ... Rick Brandt, Microsoft Access MVP ...
    (comp.databases.ms-access)