drtwqc3744 2018-12-05 03:41
浏览 76
已采纳

如何验证用户密码并将用户数据作为json字符串回显?

I'm working on an android app, a java app, and i called for a response from a php file uploaded on my host, and my response is ' []', i checked the php code and i dont know what the problem is, i saw some posts it's an encode problem about which i dont know nothing, can you help me please , here's my php code.

<?php 

require_once '../includes/DbConnect.php';

$response =array(); 

if($_POST['username'] && $_POST['password']){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM users WHERE username=?";
    $stmt = mysqli_stmt_init($con);
    mysqli_stmt_bind_param($stmt,"s",$username);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    if($user = mysqli_fetch_assoc($result))
    {
        $passwordCheck = password_verify($password,$user['password']);
        if($passwordCheck == false){
            $response['error'] = true; 
            $response['message'] = "Invalid username or password";  
        }
        else if($passwordCheck == true) {

            $response['error'] = false; 
            $response['id'] = $user['idUsers'];
            $response['email'] = $user['email'];
            $response['username'] = $user['username'];
            $response['country']= $user['country'];
             $response['firstname']= $user['firstname'];
              $response['lastname']= $user['lastname'];
               $response['points']= $user['points'];
        }
    }
}



echo json_encode($response);

?>
  • 写回答

1条回答 默认 最新

  • drq61040 2018-12-05 04:21
    关注

    My recommended adjustments:

    <?php     
    require_once '../includes/DbConnect.php';  // <-- change from procedural to obj-oriented
    $response['error'] = true;                 // default value
    if (!isset($_POST['username'], $_POST['password'])) { 
        $response['message'] = "Invalid username or password";
    } elseif (!$con) {
        $response['message'] = "Database Connection Error: "; // for private debugging only: . $con->connect_error;
    } elseif (!$stmt = $con->prepare("SELECT * FROM users WHERE username = ?")) {
        $response['message'] = "Prepare Syntax Error";  // for private debugging only: . $con->error
    } elseif (!$stmt->bind_param("s", $_POST['username']) || !$stmt->execute() || !$result = $stmt->get_result()) {
        $response['message'] = "Statement Error";  // for private debugging only: . $stmt->error
    } elseif (!$user = $result->fetch_assoc() || !password_verify($_POST['password'], $user['password'])) {
        $response['message'] = "Invalid username or password";  
    } else {
        $response['error'] = false; 
        $response['id'] = $user['idUsers'];
        $response['email'] = $user['email'];
        $response['username'] = $user['username'];
        $response['country'] = $user['country'];
        $response['firstname'] = $user['firstname'];
        $response['lastname'] = $user['lastname'];
        $response['points'] = $user['points'];
    }
    echo json_encode($response);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动