出於某種原因,登錄我的網站必須進行兩次才能工作。如果任何人有任何想法,爲什麼我欣賞它。我必須登錄兩次
這裏是我對授權的代碼:
<?php
session_start();
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
require_once(SITE_ROOT.'includes/exceptions.php');
require_once(SITE_ROOT.'data/model.php');
/*
* The purpose of this class is to manage
* access to the application, making sure the
* users are logged in before they can access
* certain features
*/
class Auth extends Model
{
function isUserLoggedIn()
{
/*
* Check for the user_id in $_SESSION
* and see if it's the database. Return
* true or false
*
*/
if(isset($_SESSION['user']))
{
return true;
}
else
{
return false;
}
}
static function redirectToLogin()
{
header("location: http://". DOMAIN .APP_DIR . "index.php?action=login");
}
static function redirectToMain()
{
header("location: http://". DOMAIN . APP_DIR . "index.php?action=main");
}
static function login($user)
{
/*
* Authenticate the user passing to the function
* a instance of the User object
*/
try
{
$db = parent::getConnection();
$pass = $user->getPassword();
$query = "select username, password from users where username = '".$user->getUsername()."' and password = '".$user->getPassword()."'";
$results = $db->query($query);
if(empty($results)) {
throw new Exception('There was a problem logging you in', EX_LOGIN_ERROR);
}
$row = $results->fetch_assoc();
$user = $row['username'];
$_SESSION['user'] = $user;
}
catch(Exception $e){
throw $e;
}
}
static function logout()
{
$old_user = $_SESSION['user'];
unset($_SESSION['user']);
session_destroy();
}
}
?>
THX
嘗試在第一次登錄後刷新頁面(不要重新輸入數據;只需在URL欄上按ENTER鍵)。它會讓你登錄嗎?如果是這樣,你的bug是在其他地方(可能在你的控制器)。 – strager 2009-02-14 21:49:54