AFNetworking PostPath php Parameters are null
- by Alejandro Escobar
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.