2012-10-28 58 views
3

我想從使用AFNetworking框架的iOS應用程序發送用戶名和密碼到php腳本。 iOS應用程序將繼續接收我定義爲「參數不足」的狀態碼401。我曾嘗試將php腳本的「用戶名」返回到iOS應用程序並接收。AFNetworking PostPath php參數爲空

基於我到目前爲止一直在調查,就好像:

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

你,如果你做了什麼的var_dump(json_decode(的file_get_contents( 「PHP://輸入」))); ?? – GBD

+0

對象(stdClass的)#3(2){ [ 「用戶名」] => 串(14) 「alejandroe1790」 [ 「密碼」] => 串(4) 「測試」 } –

回答

3

問題是,您正在將參數編碼設置爲JSON,並且它們被AFHTTPClient類設置爲HTTP正文。您可以解決此問題並通過不將編碼設置爲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; 
} 
+0

我嘗試都建議並收到「網絡連接丟失」作爲錯誤的本地化描述。我使用了var_dump(json_decode(file_get_contents(「php:// input」))); (「json編碼仍然包含在內)並收到」object(stdClass)#3(2){ [「username」] => string(14)「alejandroe1790」 [「password」] => string(4)「測試「 }」 –

+0

是的,您可以將該對象分配給一個變量,即'$ user_info',然後將您的代碼更改爲使用'$ user_info-> username'。我在本地測試了兩個我的例子,並沒有問題。第一個代碼,我刪除了參數編碼,並使用相同的PHP,你有。在第二個示例中,我使用了修改後的PHP代碼的Objective-C代碼。 –

+0

無法使其正常工作。也許我錯過了從PHP文件中的東西。我是否需要在php文件的開頭添加任何內容? –