2014-01-19 106 views
1

我有一個php表單,當用戶提交的憑據不正確時,會獲取相應的錯誤消息,但是當使用正確的憑證時,登錄成功消息不會獲得相同的錯誤憑證消息。我已經嘗試使用哈希密碼和沒有運氣。這裏是我的登錄功能代碼:PHP登錄表單不會登錄具有正確憑據的用戶

function login($username, $password) { 

$host = 'localhost'; 
$user = 'jamaixan_bobsled'; 
$pass = 'v67fvg7gk_&g'; 
$db = 'db_for_site_67'; 
mysql_connect($host, $user, $pass); 
mysql_select_db($db); 
$connected = mysql_select_db($db); 

$user_id = user_id_from_username ($username); 
$password = md5($password); 

return(mysql_result(mysql_query("SELECT COUNT(`id_user`) FROM `table_of_users` WHERE 

`email_user` = `$username` AND `password_user` = `$password`") , 0) == 1) ? $user_id : `false`; 
    } 
+0

把一個'if'語句中的每個'mysql'功能之後,以確保它返回你期望的值。我可以在那裏看到四處沒有檢查到返回值。此外,此代碼中存在SQL注入漏洞,因此請勿將其存活。 – halfer

回答

1

你不應該把用戶名和密碼值反引號,把它們放在引號中。您甚至將false置於反推。這樣做:

$result = mysql_query("SELECT COUNT(`id_user`) FROM `table_of_users` WHERE 
      `email_user` = '$username' AND `password_user` = '$password'"); 

return (mysql_num_rows($result)>0) ? $user_id : false; 
+0

仍然沒有登錄成功。我只是在我的index.php頁面調用這個函數,所以不要這樣,它仍然是登錄函數的問題。 – smoggers

+0

看看更新plz –

0

使用mysqli_ *或PDO ... MySQL是過時

function login($username, $password) 
{ 
    $cn = mysqli_connect('localhost', 'jamaixan_bobsled', 'v67fvg7gk_&g', 'db_for_site_67') or die('Connection error!'); 

    $SQL = "SELECT COUNT(id_user) FROM table_of_users WHERE email_user = '%s' AND password_user = '%s'"; 

    $Query = sprintf($SQL, mysqli_real_escape_string($username), md5($password)); 

    $Result = mysqli_query($cn, $Query) or die(mysqli_error($cn)); 

    $Rows = mysqli_fetch_array($Result); 

    return (isset($Rows[0]) && ($Rows[0] > 0)); 
} 
+0

是啊,我從幾年後的教程回來,這就是爲什麼我使用MYSQL。但是,嘗試了你的建議方法,但仍然沒有成功。不知道問題出在哪裏,因爲我只是在index.php頁面中調用登錄函數。 – smoggers

-1
function connectdb() 
{ 
    $dbhost='localhost'; 
    $dbname='db_for_site_67'; 
    $dbuser="jamaixan_bobsled"; 
    $pwd="v67fvg7gk_&g"; 

    try 
    { 
     $db=mysql_connect($dbhost,$dbuser,$pwd); 
     if($db) 
     { 
      try 
      { 
       $dbselect=mysql_select_db($dbname,$db); 
       if(!$dbselect) 
       { 
        throw new Exception("Unable to select database: $dbname with error ". mysql_error()); 
       } 
      } 
      catch(Exception $e) 
      { 
       echo $e->getMessage(); 
      } 
     } 
     else 
     { 
      throw new Exception("Unable to connect to database with error ". mysql_error()); 
     } 
    } 
    catch(Exception $e) 
    { 
     echo $e->getMessage(); 
    } 
    return $db; 
} 


function login($username, $password) { 

$user_id = user_id_from_username ($username); 
$password = md5($password); 
$conn1=connectdb(); 
$sql1="SELECT COUNT(`id_user`) FROM `table_of_users` WHERE `email_user` = $username AND `password_user` = $password"; 
$res1=mysql_query($sql1); 
$res1=mysql_fetch_array($res1); 
$res1=$res1['COUNT(`id_user`)']; 
if($res1==1){ 
    echo "login sucessful"; 
} else { 
    echo "login Failed"; 
} 

    } 
+0

爲什麼'$ username'和'$ password'是反引號? –

+0

什麼是反引號?我只是分開了連接功能。我沒有得到你的意思 –

+0

反推意味着' –