我成功制作了一个成功的注册表单,该表单使用PDO查询将信息提交到数据库,并且在用户注册等之后成功创建了会话。
在登录表单上登录时,尽管数据库中确实存在错误消息,但我不断收到错误消息“用户不存在”。我似乎无法理解为什么会发生该错误,因为我使用了类似的代码进行注册。
以下是网站前端的HTML / PHP代码。
<?php
//allow the config
define('__CONFIG__', true);
//require the config
require_once "inc/config.php"; //possibly have to change the location
?>
<!DOCTYPE html>
<html>
<head>
<!-- UIKit CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-rc.24/css/uikit.min.css" />
<title>Login Page</title>
<base href="/"/>
</head>
<body>
<div class="uk-section uk-container">
<div class="uk-grid uk-child-width-1-3@s uk-child-width-1-1" uk-grid></div>
<h2><b><center>Login</center></b></h2>
<form style="border: 3px solid #ddd; border-radius: 10px; padding: 10px;" class="uk-form-stacked js-login">
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text"><b>Email:</b></label>
<div class="uk-form-controls">
<input class="uk-input" id="form-stacked-text" type="email" required='required' placeholder="Insert Email">
</div>
</div>
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text"><b>Password:</b></label>
<div class="uk-form-controls">
<input class="uk-input" id="form-stacked-text" type="Password" required='required' placeholder="Insert Password">
</div>
</div>
<div class="uk-margin uk-alert uk-alert-danger js-error" style='display: none;'></div>
<div class="uk-margin">
<label><center>Don't have an account? <a href='comp1687/register.php'>Create one now!</a></center></label>
<center><button class="uk-button uk-button-default" type="submit"><b>LOGIN</b></button></center>
</div>
</form>
</div>
<?php require_once "inc/footer.php"; ?>
</body>
</html>
以下是用于验证的login.php AJAX代码。
<?php
// Allow the config
define('__CONFIG__', true);
// Require the config
require_once "../inc/config.php"; //possibly have to change the location
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Always return JSON format
// header('Content-Type: application/json');
$return = [];
$email = Filter::String( $_POST['email'] );
$password = $_POST['password'];
$user_found = User::Find($email, true);
if($user_found) {
// User exists, try and sign them in
$user_id = (int) $user_found['user_id'];
$hash = (string) $user_found['password'];
if(password_verify($password, $hash)) {
// User is signed in
$return['redirect'] = 'dashboard.php?message=welcome'; //possibly have to change the location
$_SESSION['user_id'] = (int) $user_id;
} else {
// Invalid user email/password combo
$return['error'] = "Invalid user email/password combo";
}
} else {
// They need to create a new account
$return['error'] = "You do not have an account. <a href='register.php'>Create one now?</a>";
}
echo json_encode($return, JSON_PRETTY_PRINT);
} else {
///Kill the script. Redirect the user.
exit('Invalid URL');
}
?>
最后,main.js代码如下
.on("submit", "form.js-login", function(event) {
event.preventDefault();
var _form = $(this);
var _error = $(".js-error", _form);
var dataObj = {
email: $("input[type='email']", _form).val(),
password: $("input[type='password']", _form).val(),
};
if(dataObj.email.length < 6) {
_error
.text("Please enter a valid email address")
.show();
return false;
} else if (dataObj.password.length < 8) {
_error
.text("Please enter a password that is at least 8 characters long.")
.show();
return false;
}
// Assuming the code gets this far, we can start the ajax process
_error.hide();
$.ajax({
type: 'POST',
url: 'ajax/login.php',
data: dataObj,
dataType: 'json',
async: true,
})
.done(function ajaxDone(data) {
// Whatever data is
if(data.redirect !== undefined) {
window.location = data.redirect;
console.log('data');
} else if(data.error !== undefined) {
_error
.html(data.error)
.show();
console.log('data');
}
})
.fail(function ajaxFailed(e) {
//this failed
console.log(e);
})
.always(function ajaxAlwaysDoThis(data) {
//always do
console.log('Always');
})
return false;
})
具有查找功能的用户查找类
public static function Find($email, $username, $return_assoc = false) {
$con = DB::getConnection();
// Make sure the user does not exist.
$email = (string) Filter::String( $email );
$findUser = $con->prepare("SELECT user_id, password FROM users WHERE email = (:email) AND username = (:username) LIMIT 1");
$findUser->bindParam(':email', $email, PDO::PARAM_STR);
$findUser->bindParam(':username', $username, PDO::PARAM_STR);
$findUser->execute();
if($return_assoc) {
return $findUser->fetch(PDO::FETCH_ASSOC);
}
$user_found = (boolean) $findUser->rowCount();
return $user_found;
}
答案 0 :(得分:0)
$scope.isLoadingIndex = null;
$scope.donwload = function($index) {
$scope.isLoadingIndex = $index;
//Rest of your code...
}
//添加以下两行:
//Are you receiving some correct ouput from your php code?
//To check it, after this code,
else {
// They need to create a new account
$return['error'] = "You do not have an account. <a href='register.php'>Create one now?</a>";
}
//并将输出发送给我。
答案 1 :(得分:0)
您要将“ true”作为第二个变量传递给User::Find
方法,然后将其解释为MySQL查询中的字符串文字。 (“真”)
您是否使用电子邮件作为用户名或单独的用户名字段?我在您的HTML中找不到。如果您仅使用电子邮件,则可以摆脱Find方法中的第二个参数,并删除这两个参数:
AND username = (:username)
$findUser->bindParam(':username', $username, PDO::PARAM_STR);