2012-02-23 20 views
2

我想通過從我的iOS應用程序向PHP腳本發送POST請求來驗證用戶。然後讓iOS應用程序讀取NSHTTPURLResponse以確定它是否成功。我的iOS代碼如下所示,出於某種原因,它說我的NSURLConnection未使用。通過int代碼總是回拍攝的200試圖通過發送用戶名/密碼到PHP腳本來驗證iOS用戶

- (void)authenticateUser:(NSString *)aUsername 
       password:(NSString *)aPassword 
{ 
    NSString *post = [NSString stringWithFormat:@"username=%@&pw=%@", aUsername, aPassword]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [post length]]; 

    NSURL *url = [NSURL URLWithString:@"http://XXX.com/query-db.php"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 
    int code = [httpResponse statusCode]; 

    // Log the response 
    NSLog(@"%d", code); 
    if(code == 1){ 
     NSLog(@"received a 1"); 
     [self updateWorkTime]; 
     //[self close]; 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsuccessul Attempt" message:@"The username or password is invalid, please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

的值。此外,如果你想看看我的PHP腳本,這是我在爲虛擬變量輸入並測試,看它是否正常查詢的數據庫和檢查出來...但萬一你好奇。

<?php 
    $sentUsername = $_POST['username']; 
    $sentPW = $_POST['pw']; 

    mysql_connect("xxx", "xxx", "xxx") or die(mysql_error()); 
    mysql_select_db("xxx") or die(mysql_error()); 

    $result = mysql_query("SELECT * FROM Table") or die(mysql_error()); 

    $authenticate = 0; 

    while($authenticate == 0) { 
     $row = mysql_fetch_array($result); 
     $selectedUsername = $row['username']; 
     $selectedPW = $row['pw']; 
     $pwComp = strcmp($sentPW, $selectedPW); 
     $userComp = strcmp($selectedUsername, $sentUsername); 

     if(!$row) break; 
     if($selectedPW == $sentPW && $selectedUsername == $sentUsername) { 
      $authenticate = 1; 
     } 
    } 
    echo $authenticate; 
?> 

我試圖設置PHP頭,但我不確定如何做到這一點。我很感謝這個社區給予我的任何和所有幫助,這真是無價。謝謝!

回答

2

你得到後面的「200」代碼HTTP狀態代碼,表明請求成功可見。

如果您要從PHP腳本發回'1'或'0'值,您應該在didReceiveData:委託方法中尋找它,而不是didReceiveResponse:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData 
{ 
    NSLog(@"authenticate value => %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]); 
} 
+0

感謝您的迴應,我在'1'或'0'之後......您如何推薦獲得它?現在,響應是即時通信的整個html/php文件。 – thebiglebowski11 2012-02-24 02:21:59

+0

此外,我didReceiveData方法似乎並沒有完成...任何想法爲什麼會是這樣的情況? – thebiglebowski11 2012-02-24 02:26:40

+0

響應還有什麼?在你的PHP代碼中你的問題看起來像你只寫出一個字符:'echo $ authenticate;' – jonkroll 2012-02-24 02:27:11

0

你正在使用NSASCIIStringEncoding - 嘗試使用NSUTF8StringEncoding - 你可能會失去它在轉換到NSData對象。

此外,您應該使用HTTPS,否則你的密碼是所有:-)