我已经使用php和myqli进行了oop登录,我还使用jquery动态检查用户输入,当用户详细信息正确时,我的代码会记录用户但是没有发送头重定向但没有错误给出了消息,它可能与我有关,我正在使用json数组将值返回给用户。任何帮助都将受到赞赏。
登录类
<?php
require "db_config.php";
class login
{
public function user_exists($username) {
$sql="SELECT * FROM users WHERE username='$username'";
//checking if the email is available in db
$check = Database::getInstance()->getConnection()->query($sql);
$count_row = $check->num_rows;
if ($count_row == 1) {
return true;
} else {
return false;
}
}
public function check_login($username,$password){
$sql2="SELECT user_id FROM users WHERE username='$username' and password='$password'";
//checking if the username is available in the table
$check = Database::getInstance()->getConnection()->query($sql2);
$count_row = $check->num_rows;
if ($count_row == 1) {
$_SESSION['username'] = $username;
return true;
}
else {
return false;
}
}
public function is_loggedin()
{
if(isset($_SESSION['user_session']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
}
?>
登录脚本
<?php
require '../includes/class/login.php';
$login = new login();
$username_error=' ';
$password_error=' ';
$result=' ';
if (isset($_POST['login'])&& $_POST['login'] !== "")
{
$log = $login->check_login($username,$password);
if ($log)
{
$login->redirect('home.php');
}
else
{
// Registration Failed
$password_error='Wrong username or password';
}
}
if(isset($_POST['username']) && $_POST['username'] !== "")
{
$username = $_POST['username'];
$username = trim($username);
$user_exists = $login->user_exists($username);
if ($user_exists) {
$username_error = '';
}
else {
$username_error = 'username not found';
}
}
if(isset($_POST['password']) && $_POST['password'] !== "") {
$password = $_POST['password'];
$password = trim($password);
$password_check = $login->check_login($username,$password);
if ($password_check) {
$password_error = '';
}
else {
$password_error = 'password not found';
}
}
$return_data=array('username'=> $username_error, 'password'=> $password_error ,'user_session'=>$_SESSION['username']);
header('Content-Type: application/json');
echo json_encode($return_data);
?>
登录js代码
<?
function validate(username, password, login)
{
$.post('includes/login.php', {username: username,password: password,login: login,},
function(data){
$('#username_feedback').text(data.username);
$('#password_feedback').text(data.password);
$('#login_feedback').text(data.user_session); },'json');
}
/////////////////////////////////username jquery///////////////////////////////////
$('#username').focusin(function(){
if($('#username').val() ==='') {
$('#username_feedback').text('requried field');
} else {
validate($('#username').val(),"","");
}
}).blur(function() {
$('#username_feedback').text('');
validate($('#username').val(),"","");
}).keyup(function(){
validate($('#username').val(),"","");
});
/////////////////////////////////password jquery///////////////////////////////////
$('#password').focusin(function(){
if($('#password').val() ==='') {
$('#password_feedback').text('requried field');
} else {
validate( $('#username').val(), $('#password').val(),"");
}
}).blur(function() {
$('#password_feedback').text('');
validate( $('#username').val(), $('#password').val(),"");
}).keyup(function(){
validate( $('#username').val(), $('#password').val(),"");
});
$( "#login" ).click(function() {
validate($('#username').val(),$('#password').val(),$('#login').val());
});