AmySession::create PHP Метод

create() публичный Метод

public create ( $userId = 1 )
    public function create($userId = 1)
    {
        AmyLogger::logn('Creating session for:', $userId);
        if (!is_numeric($userId)) {
            AmyLogger::logn("Invalid user ID specified: `{$userId}'");
            throw new Exception("Invalid user ID specified: `{$userId}'");
        }
        try {
            $this->authorize();
            if ($userId == $this->authInfo['user_id']) {
                AmyLogger::logn("Already authorized, renewing.", $this->authInfo);
                if (time() + self::RENEW_BEFORE_EXPIRATION < $this->authInfo['expired_at']) {
                    AmyLogger::logn("Not necessary to renew yet.");
                    // not renewing until one hour before expiration to avoid overhead
                    return $this;
                }
                // let's renew it
                $this->authInfo['expired_at'] = time() + self::EXPIRES_AFTER_SECONDS;
                $this->save();
                AmyLogger::logn("Renewed successfully");
                return $this;
            }
        } catch (Exception $e) {
        }
        $this->authInfo['user_id'] = $userId;
        $this->authInfo['expired_at'] = time() + self::EXPIRES_AFTER_SECONDS;
        $this->authInfo['token'] = $this->generate_hash();
        $this->save();
        AmyLogger::logn('Setting cookie for newly created session: ', $this->authInfo);
        setcookie('amy_token', $this->authInfo['token'], 0, '/');
        return $this;
    }

Usage Example

Пример #1
0
 public function create_session()
 {
     if (!$this->is_authorized()) {
         throw new Exception('User is not authorized. Could not create session.');
     }
     $session = new AmySession($this->configuration);
     $session->create($this->userId);
     $this->save_user_to_session($session);
     $this->session = $session;
     return $session;
 }