Example #1
0
 public function startSession($cookieDuration = 0)
 {
     if (count($this->sessions) >= User::config('max_sessions')) {
         return;
     }
     //Throw exception? replace oldest? check if current ip is one of them..?
     if (!is_int($cookieDuration) && !ctype_digit($cookieDuration)) {
         throw new UserIncorrectDatatypeException('startSession()', 1, 'integer', $cookieDuration);
     }
     if ($cookieDuration < 0) {
         throw new UserNegativeValueException('startSession()', $cookieDuration);
     }
     //Ready session data...
     $sessionKey = User::generateSessionKey();
     $hashedKey = hash(User::config('hash_algorithm'), $sessionKey);
     $sessionIP = $_SERVER['REMOTE_ADDR'];
     //Send session cookies...
     User::sendCookies($this->username, $sessionKey, $cookieDuration);
     //Update database...
     $db = User::getDB();
     $query = $db->prepare('INSERT INTO usersSessions(userID, key, IP) VALUES(:id, :key, :IP)');
     $query->bindParam(':key', $hashedKey, PDO::PARAM_STR);
     $query->bindParam(':IP', $sessionIP, PDO::PARAM_STR);
     $query->bindParam(':id', $this->id, PDO::PARAM_INT);
     $query->execute();
     //Add/update session in $sessions array
     $this->sessions[$hashedKey] = $sessionIP;
     User::processEventHandlers('onSessionStart', $this);
 }