2012-12-16 3 views
1

사용자 이름과 암호가있는 간단한 로그인 페이지 (index.php)가 있습니다. 모든 데이터베이스 관련 기능을 가진 db_connect.php라는 파일을 만들었습니다.PHP MySQL 로깅 문제

class Database{ 

    private $connection; 
    private $db; 

    function __construct() 
    { 

     $this->connection = mysql_connect('localhost','root','') or die("cannot connect"); 
     $this->db = mysql_select_db('logistics',$this->connection); 
    } 

    function verify_login($uid,$password) 
    { 
     $verify_login = "select user_id from log_users where user_login = '$uid' and user_password = '$password'";     
     $status = mysql_query($verify_login,$this->connection); 
     while($result = mysql_fetch_assoc($status)){ 
     if($result['user_id'] == '1') 
      { 
       return true; 
      } 
      else{ 
       return false; 
      } 
    } 
} 

이제 verify_login ($ username, $ password) 함수를 호출하는 데 사용되는 클래스 데이터베이스의 개체를 만들었습니다. 여기서 문제는 로그인의 상태와 상관없이 출력이 빈 페이지라는 것입니다. 변수 $ status를 출력하면 리소스 ID를 반환합니다. 하지만 아무것도 반환되지 않습니다. 놀라운

+0

? 당신의 함수가 적합 할 때 true/false를 리턴하는지 알 수 있습니까? –

답변

0

: SQL 주입 ... 아니, 난 /하지

or die("cannot connect"); 

사용 예외를 시도 catch 블록

$verify_login = "select user_id from log_users where user_login = '$uid' and user_password = '$password'"; 

오. 왜? uid는 정수 값 또는 문자열입니까? 문자열 이름 변수가 u_login 또는 out ID가있는 다른 문자열 일 경우 하지만 정수라면 ... $ uid 근처에서 use (int)를 사용하면 정수형의 입력 데이터를 얻을 수 있습니다. $는 청소해야 user_passrord : mysql_real_escaping 우리에게보다 더

$verify_login = "select user_id from log_users where user_login = '$uid' and user_password = 'mysql_real_escape_string($password)'"; 

도움 :

if(!empty($result)) { 
    return true; // auth 
}else{ 
    return false; // error auth 
} 

some_file.php :

코드에서 로그인 성공 메시지 표시입니다해야하는 경우
// include your code 

if(verify_login($login, $password)) { 
    echo 'Auth!'; 
}