Omer Waqas Khan
Omer Waqas Khan

Reputation: 2413

Authenticating on php script from iOS/iphone application

This is my php where I need to authenticate:

<?php

        $username=$_POST["m_username"];
        $password=$_POST["m_password"];
/*      
        $username=$_GET["m_username"];
        $password=$_GET["m_password"];
*/

?>

I am using below code to authenticate from my iPhone app. But it is not authenticating. I dont know what is the issue that its not working. It says null parameters/values sent.

NSString *urlAsString =[NSString stringWithFormat:@"http://www.myurl.com/abc/authenticate.php"];

NSURL *url = [NSURL URLWithString:urlAsString];

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; 

[urlRequest setTimeoutInterval:30.0f];

[urlRequest setHTTPMethod:@"POST"];

[urlRequest addValue:@"test" forHTTPHeaderField:@"m_username" ];

[urlRequest addValue:@"123" forHTTPHeaderField:@"m_password" ];

[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {

    if ([data length] >0 && error == nil){

        html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"HTML = %@", html);

        receivedData = [NSMutableData data];

    }
    else if ([data length] == 0 && error == nil){

        NSLog(@"Nothing was downloaded."); 

    }

    else if (error != nil){

        NSLog(@"Error happened = %@", error);
    } 
}];

// Start loading data
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (theConnection) 
{
    // Create the NSMutableData to hold the received data
    receivedData = [NSMutableData data];

} 
else {
    // Inform the user the connection failed.

}

Upvotes: 0

Views: 779

Answers (3)

Dan Ray
Dan Ray

Reputation: 21893

[urlRequest addValue:@"test" forHTTPHeaderField:@"m_username" ];

[urlRequest addValue:@"123" forHTTPHeaderField:@"m_password" ];

This is your problem. You're sending your values as HTTP header fields, not as post data. Post data goes in the request body, not the header. So PHP doesn't treat it as incoming post fields to look at in the $_POST array. (Those fields might turn up in the $_SERVER array, though... I've never tried that. It's not the preferred method for sending post data, anyhow.)

Do this instead:

NSString *myRequestString = @"m_username=test&m_password=123";
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] 
                                        length: [myRequestString length]];

[urlRequest setHTTPBody: myRequestData];

You can, obviously, compose myRequestString as a formatted string, dropping in values the user provides, if needed.

Also note that since you're hitting a non-SSL URL, this data will be sent in the clear, which is known by all the kids these days as A Bad Idea.

Upvotes: 1

Paul de Lange
Paul de Lange

Reputation: 10633

From the iPhone you are passing parameters in the HTTP header, but in the PHP script you are retrieving the parameters from POST data. Try synchronizing your data transfer.

Upvotes: 0

Tuhin Subhra Dey
Tuhin Subhra Dey

Reputation: 980

Try calling your php page url like this

[your url]?m_username=abc&m_password=abc then change

$_POST['m_username'] to $_REQUEST['m_username'] 
and 
$_POST['m_password'] to $_REQUEST['m_password'] 

See what you get...if its okay with the json data then there the problem is in your cocoa code.

Best of luck.

Upvotes: 0

Related Questions