首页 > 解决方案 > PHP:如何无法读取函数返回值

问题描述

我想用模式引导和 PHP 和 jQuery 制作登录系统,我在点击登录按钮后显示了这个模式:

<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalCenterTitle">Login Page</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
            <div class="form-group">
                <label for="exampleInputEmail1">Username</label>
                <input type="text" name='username' class="form-control" id="username">
            
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">Password</label>
                <input type="password" name='password' class="form-control" id="password">
            </div>
            <div class="form-group form-check">
                <input name='remember' type="checkbox" class="form-check-input" id="remember">
                <label class="form-check-label" for="exampleCheck1">Remember me</label>
            </div>
            <button type="button" name='login' id='login' class="btn btn-primary mr-auto">Login</button>
      </div>
    </div>
  </div>
</div>

所以我有这个依赖于用户类的登录功能:

function login($username ,$password)
{
    $database = new Database();
    $db = $database->connect();
    $user = new User($db);    
    $user->username=$username;    
    if($user->check_user()){    
        $user->get_profile();    
        if( md5($password)==$user->password){    
            return 'logined successfully';
        } else {
            return 'password is wrong';
        }
    } else {
        return 'username is wrong';
    }
}

login.php 页面代码是:

<?php
include_once('../includes/functions.php');
    //login
if(isset($_POST['login'])){    
    $username = $_POST['username'];
    $password = $_POST['password'];
    $remember = $_POST['remember'];    
    $message=login($username ,$password); /// the error is here   
    if($message=='logined successfully' && $remember =='on'){
        $domain = ($_SERVER['HTTP_HOST'] != 'localhost:3308') ? $_SERVER['HTTP_HOST'] : false;
        setcookie("username",$username,time()+31556926, '/', $domain, false );
    } elseif($message=='logined successfully' && $remember !='on') {
        session_start();
        $_SESSION['MM_Username'] = $username;            
    }
}
echo $message;
// end of login
?>

和JavaScript是:

<script>
$(document).ready(function(){
    $('#login').click(function(){
        var username =$('#username').val();
        var password =$('#password').val();
        if(document.getElementById("remember").checked==true){
            var remember ='on';
        } else {
            var remember ='off';
        }           
        if(username!='' && password!=''){
            console.log(remember);
            $.ajax({
                url:'login.php',
                method : 'POST',
                data :{username:username,password:password,remember:remember},
                success:function(data){
                    if(data =='logined successfully'){
                        console.log(data);
                        $('#loginModal').hide();
                        location.reload();                          
                    } else {
                        console.log(data);
                        $('#loginModal').hide();
                        alert(data);                                
                    }
                }
            });
        } else {
            alert('Both username and password required!');
        }
    })
})
</script>

当我在控制台上记录数据值时,它告诉我来自 login.php 的消息变量未定义,我不知道我的错误在哪里

标签: phpjqueryauthentication

解决方案


您在第 4 行的帖子变量中使用“登录”索引,但在 jQuery ajax 中,您没有在请求中传递任何登录数据

因此,在您的 php 文件中完成处理后,没有用于回显的 $message 变量,并且您的请求返回为空

将其用作 jQuery ajax 中的数据(我的建议):

data: { login: 1, username: username, password: password, remember: remember },

或者在 php 文件中使用它而不是“isset($_POST['login'])”:

if(isset($_SERVER['REQUEST_SCHEME']) AND $_SERVER['REQUEST_SCHEME'] == 'POST'){

推荐阅读