3

我正在尝试使用 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;
}

我觉得我可能错过了什么。任何帮助都会很棒。

4

1 回答 1

3

问题是您将参数编码设置为 JSON,并且它们被AFHTTPClient类设置为 HTTP 正文。您可以$_POST通过不将编码设置为 JSON 来解决此问题并使用它。

- (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;
}
于 2012-10-28T05:41:52.963 回答