我對php比較陌生,我試圖編寫一個非常簡單的登錄腳本。我已經獲得了基本的功能,但我無法登錄到系統。我的登錄腳本在下面,我的註冊腳本也在下面。php登錄身份驗證失敗
checklogin.php
include_once 'inc/db.inc.php';
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['password']));
try {
$sql="SELECT id, username, password FROM users WHERE username='$username' and password='$password'";
$result = $pdo->query($sql);
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count == 1){
// Register $username, $password and redirect to file "index.php"
session_register("username");
session_register("password");
header("Location: index.php");
}
else {
header("Location: login.php?invalid=1");
}
}
catch (PDOException $e) {
echo $e;
}
ob_end_flush();
?>
checkreg.php
include_once 'inc/db.inc.php';
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['password'] | !$_POST['passwordconf']) {
die('You did not complete all of the required fields');
}
if ($_POST['password'] != $_POST['passwordconf']) {
die('Your passwords did not match. ');
}
$_POST['password'] = md5($_POST['password']);
if (!get_magic_quotes_gpc()) {
$_POST['password'] = addslashes($_POST['password']);
$_POST['username'] = addslashes($_POST['username']);
}
$username = $_POST['username'];
$password = $_POST['password'];
try {
// now we insert it into the database
$sql = "INSERT INTO users(username,password) VALUES ('$username','$password')";
$result = $pdo->exec($sql);
header("Location: index.php");
} catch (PDOException $e){
echo $e;
}
?>
我知道該登記寫入數據庫,但每次我嘗試我收到我的證書無效標誌有效的登錄。任何你能做的事情都會很棒。謝謝。
似乎不像你阻止人們註冊相同的用戶名/密碼組合。你在桌上碰巧有不止一次相同的組合?這會阻止$ count從1,並導致你所看到的。 – Gyhth 2013-02-27 19:10:17
您是否嘗試在登錄後嘗試回顯用戶名和密碼以查看它們是否與數據庫匹配? – James 2013-02-27 19:11:42