【发布时间】: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