我正在尝试使用AFNetworking框架从iOS应用程序发送用户名和密码到php脚本。 iOS应用程序继续接收状态代码401,我将其定义为“参数不足”。我已经尝试将“用户名”从php脚本返回到iOS应用程序并接收。
基于我到目前为止所研究的内容,似乎是:
1)php脚本没有正确解码POST参数
2)iOS应用程序未正确发送POST参数
以下是iOS功能
- (IBAction)startLoginProcess:(id)sender
{
NSString *usernameField = usernameTextField.text;
NSString *passwordField = passwordTextField.text;
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:usernameField, @"username", passwordField, @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost/~alejandroe1790/edella_admin/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient defaultValueForHeader:@"Accept"];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient postPath:@"login.php" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error with request");
NSLog(@"%@",[error localizedDescription]);
}];
}
以下是php脚本
function checkLogin()
{
// Check for required parameters
if (isset($_POST["username"]) && isset($_POST["password"]))
{
//Put parameters into local variables
$username = $_POST["username"];
$password = $_POST["password"];
$stmt = $this->db->prepare("SELECT Password FROM Admin WHERE Username=?");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($resultpassword);
while ($stmt->fetch()) {
break;
}
$stmt->close();
// Username or password invalid
if ($password == $resultpassword) {
sendResponse(100, 'Login successful');
return true;
}
else
{
sendResponse(400, 'Invalid Username or Password');
return false;
}
}
sendResponse(401, 'Not enough parameters');
return false;
}
我觉得我可能会遗漏一些东西。任何帮助都会很棒。
答案 0 :(得分:3)
问题是您正在将参数编码设置为JSON,并且AFHTTPClient
类将它们设置为HTTP Body。您可以通过不将编码设置为JSON来解决此问题并使用$_POST
。
- (IBAction)startLoginProcess:(id)sender
{
NSString *usernameField = usernameTextField.text;
NSString *passwordField = passwordTextField.text;
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:usernameField, @"username", passwordField, @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost/~alejandroe1790/edella_admin/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient defaultValueForHeader:@"Accept"];
[httpClient postPath:@"login.php" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error with request");
NSLog(@"%@",[error localizedDescription]);
}];
}
但是,如果您愿意,可以将参数作为JSON字符串发送,但是您将无法使用$_POST
,并且需要json_decode
$HTTP_RAW_POST_DATA
function checkLogin()
{
global $HTTP_RAW_POST_DATA;
// remove the second argument or pass false if you want to use an object
$user_info = json_decode($HTTP_RAW_POST_DATA, true);
// Check for required parameters
if (isset($user_info["username"]) && isset($user_info["password"]))
{
//Put parameters into local variables
$username = $user_info["username"];
$password = $user_info["password"];
$stmt = $this->db->prepare("SELECT Password FROM Admin WHERE Username=?");
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($resultpassword);
while ($stmt->fetch()) {
break;
}
$stmt->close();
// Username or password invalid
if ($password == $resultpassword) {
sendResponse(100, 'Login successful');
return true;
}
else
{
sendResponse(400, 'Invalid Username or Password');
return false;
}
}
sendResponse(401, 'Not enough parameters');
return false;
}