2013-08-25 44 views
0

有谁知道什么可能会出错?我尝试了一切,搜索了一切,但什么都没找到。有可能是我看不到的拼写错误。PHP - 错误 - MySQL

功能类

function validate_credentials($user_name, $user_password) { 
    $user_name  = mysql_real_escape_string($user_name); 
    $user_password = sha1($user_password); 

    $result = mysql_query("SELECT `user_id` FROM `users` WHERE `user_name` = '{$user_name}' AND `user_password` = '{$user_password}'"); 

    if (mysql_num_rows($result) !== 1) { 
     return false; 
    } 

    return mysql_result($result, 0); 
} 

include('./core/user.inc.php'); 

if (isset($_POST['user_name'], $_POST['user_password'])) { 
    if (($user_id = validate_credentials($_POST['user_name'], $_POST['user_password'])) != false) { 
     $_SESSION['user_id'] = $user_id; 

     header('Location: ./messages/'); 

     die(); 
    } 
} 

这是我的HTML:

<form method="post" action="./sign-in.php"> 
    <input type="text" name="user_name" id="user_name" placeholder="Username" /> 
    <input type="password" name="user_password" id="user_password" placeholder="Password" /> 
    <input type="submit" placeholder="Confirm Password" value="Sign in" /> 
</form> 
+1

当你运行它会发生什么?任何错误消息? –

+0

我使用谷歌浏览器,它只是说'服务器错误'。 –

+0

**我发现'if(($ user_id = validate_credentials($ _ POST ['user_name'],$ _POST ['user_password']))!= false){'导致错误** –

回答

0

你可以尝试

$user_id = validate_credentials($_POST['user_name'], $_POST['user_password']) 

if($user_id){ 
    $_SESSION['user_id'] = $user_id; blahblah} 
+0

这没有奏效。同样的事情发生 –

+0

你确定密码是用sha1插入和加密的吗?也可以使用其他浏览器。 – Mihai

+0

哦,但不应该该函数包含连接到MySQL,否则选择不起作用。 – Mihai

0
function validate_credentials($user_name, $user_password) { 
    $user_name = trim(mysql_real_escape_string($user_name)); 
    $user_password = trim(mysql_real_escape_string(sha1($user_password))); 

    $result = @mysql_query("SELECT user_id FROM users WHERE user_name = '".$user_name."' AND user_password = '".$user_password."'"); 

    if (mysql_num_rows($result) != 1) { 
     return false; 
    } 
    return mysql_result($result, 0); 

} 

include('./core/user.inc.php'); 

if (isset($_POST['user_name']) && isset($_POST['user_password'])) { 
    $user_id = validate_credentials($_POST['user_name'], $_POST['user_password']); 
    if ($user_id) { 
     $_SESSION['user_id'] = $user_id; 
     header('Location: ./messages/'); 
     die(); 
    } 
} 
+0

我明白了,脚本不喜欢从另一个类访问函数。 –

+0

奇怪?!但它的工作。 –

+0

你检查$ user_id变量是否为空? – user2727841