【问题标题】:Empty POST on Angular/PHPAngular/PHP 上的空 POST
【发布时间】:2017-12-08 00:42:59
【问题描述】:

我是 Angular 4 的新手。我添加了一个简单的 HTTP POST 登录并将其连接到另一个简单的 PHP 接收器,然后连接到数据库以检查凭据是否正确。

错误代码:

    login(username: string, password: string){
    var headers = new Headers();
    var creds ='http://localhost/crm/login/login.php';
    headers.append('content-type', 'application/X-www-form=urlencoded');
    return this._http.post(creds, {username: username, password: password}, {headers})
        .map((response: Response) => {
            let user = response.json();
            if(user && user.accid){
                this.isLoggedIn = true;
                this.loginType = user.accid; 

                sessionStorage.setItem('accid', JSON.stringify(user.accid));
                sessionStorage.setItem('status', JSON.stringify(user.status));
                sessionStorage.setItem('username', JSON.stringify(user.user));
                sessionStorage.setItem('uniqid', JSON.stringify(user.uniqid));
                return user;
            }else
                return false;

      });
}

问题是,如果我发布,PHP 服务器根本不会读取任何输入,就好像我什么都没发布一样。

这就是我“修复”它的方式,但实际上这根本不是修复。这基本上是使用 GET,除了我格式化字符串本身。显然这是一个可怕的解决方法,因为现在我必须在 PHP 主体上接受 GET 请求,并且请求本身在登录时会在浏览器的控制台中逐字打印出来。

login(username: string, password: string){
    var headers = new Headers();
    var creds ='http://localhost/crm/login/login.php?' + 'username=' + username + '&password=' + password;
    headers.append('content-type', 'application/X-www-form=urlencoded');
    return this._http.post(creds, '', {headers})
        .map((response: Response) => {
            let user = response.json();
            if(user && user.accid){
                this.isLoggedIn = true;
                this.loginType = user.accid; 

                sessionStorage.setItem('accid', JSON.stringify(user.accid));
                sessionStorage.setItem('status', JSON.stringify(user.status));
                sessionStorage.setItem('username', JSON.stringify(user.user));
                sessionStorage.setItem('uniqid', JSON.stringify(user.uniqid));
                return user;
            }else
                return false;

      });
}

PHP 正文

<?php
require_once '../config.php';

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

$parmSir = [];
if($_GET)
    foreach($_GET as $gettie)
        $parmSir[] = $gettie;

if($_POST)
    foreach($_POST as $postie)
        $parmSir[] = $postie;

$username = isset($parmSir[0]) ? $parmSir[0] : NULL;
$password = isset($parmSir[1]) ? $parmSir[1] : NULL;

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connection, $username);
$password = sha1($password, true);
$response = [];
$query = mysqli_query($connection, "SELECT * FROM user_accounts WHERE password='$password' AND username='$username'");
$rows = mysqli_num_rows($query);

if ($rows == 1){
    $query = mysqli_query($connection, "SELECT account_type, employee_ID FROM user_accounts WHERE username ='$username'");
    $row = $query->fetch_assoc();
    $response['status'] = 'loggedin';
    $response['accid'] = $row['account_type'];
    $response['user'] = $username;
    $response['uniqid'] = $row['employee_ID'];
}
else
    $response['status'] = 'error';
echo json_encode($response);
?>

不完全确定为什么 PHP 不能识别 POST/GET 请求,除非我以这种特定方式格式化字符串。我做错了什么?

【问题讨论】:

    标签: javascript php angular login


    【解决方案1】:

    解决方案是确保标头的“内容类型”为“应用程序/json”。否则,_POST 和 _GET 将不会按要求读取内容(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type)。

    其他帖子建议使用 $HTTP_RAW_POST_DATA(现已弃用,替换为我现在使用的 php://input)。

    现在是这样的:

        login(username: string, password: string){
        var headers = new Headers();
        var creds ='http://localhost/crm/login/login.php';
        headers.append('content-type', 'application/json');
        return this._http.post(creds, {username: username, password: password}, {headers})
            .map((response: Response) => {
                let user = response.json();
                if(user && user.accid){
                    this.isLoggedIn = true;
                    this.loginType = user.accid; 
                    
                    sessionStorage.setItem('accid', JSON.stringify(user.accid));
                    sessionStorage.setItem('status', JSON.stringify(user.status));
                    sessionStorage.setItem('username', JSON.stringify(user.user));
                    sessionStorage.setItem('uniqid', JSON.stringify(user.uniqid));
                    return user;
                }else if(user.status == "error"){
                    return false;
                }else
                    return false;
    
          });
    }
    

    还有.PHP:

    <?php
    require_once '../config.php';
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
    
    if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
        throw new Exception('Request method must be POST!');
    }
    
    $input = json_decode(trim(file_get_contents('php://input')),true);
    
    $username = $input['username'];
    $password = $input['password'];
    
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysqli_real_escape_string($connection, $username);
    $password = sha1($password, true);
    $response = [];
    $query = mysqli_query($connection, "SELECT * FROM user_accounts WHERE password='$password' AND username='$username'");
    $rows = mysqli_num_rows($query);
    
    if ($rows == 1){
        $query = mysqli_query($connection, "SELECT account_type, employee_ID FROM user_accounts WHERE username ='$username'");
        $row = $query->fetch_assoc();
        $response['status'] = 'loggedin';
        $response['accid'] = $row['account_type'];
        $response['user'] = $username;
        $response['uniqid'] = $row['employee_ID'];
    }
    else
        $response['status'] = 'error';
    echo json_encode($response);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多