Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking PostPath php Parameters are null

I am trying to send a username and password from an iOS app using AFNetworking framework to a php script. The iOS app continues to receive status code 401 which I defined to be "not enough parameters". I have tried returning the "username" from the php script to the iOS app and receive .

Based on what I've been investigating so far, it seems as though:

1) The php script is not decoding the POST parameters properly

2) The iOS app is not sending the POST parameters properly

The following is the iOS function

- (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]);
             }];

}

The following is the php script

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;
}

I feel like I may be missing something. Any assistance would be great.

like image 823
Alejandro Escobar Avatar asked Feb 24 '26 17:02

Alejandro Escobar


1 Answers

The problem is that you are setting the parameter encoding to JSON and they are being set to the HTTP Body by the AFHTTPClient class. You can get around this and use $_POST by not setting the encoding to 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]);
             }];

}

However, you can send your parameters as a JSON string if you choose, but you won't be able to use $_POST and will need to 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;
}
like image 69
Chris McKnight Avatar answered Feb 26 '26 09:02

Chris McKnight