2012-02-23 20 views
2

にユーザ名/ PWを送信することによって、iOS版でユーザーを認証しようとしています。そして、iOSアプリは、それが成功したかどうかを判断するためにNSHTTPURLResponseを読んでました。私のiOSコードは以下のように表示されます。何らかの理由で私のNSURLConnectionが使用されていないと言います。あなたは私がのためのダミー変数に入力され、それが適切にデータベースを照会かどうかを確認するためにテストされ、私のPHPスクリプト、を見てみたい場合はint型のコードは常に、さらに200は、私はPHPスクリプトに私のiOSアプリからのPOSTリクエストを送信することによって、ユーザーを認証しようとしているPHPスクリプト

- (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 
    $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ヘッダーを設定しようとしましたが、その方法はわかりませんでした。このコミュニティが私に与えてくれた、本当に貴重な助けに感謝します。ありがとう!あなたはNSASCIIStringEncodingを使用している

答えて

2

すべてあなたが戻って取得している「200」のコードは、要求が成功したことを示す、HTTPステータスコードであるに表示されます。

PHPスクリプトから '1'または '0'の値が返ってきた場合はdidReceiveResponse:ではなく、didReceiveData:デリゲートメソッドで検索する必要があります。

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

レスポンスのおかげで、私は「1」または「0」の後にしています...どのようにそれを得ることをお勧めしますか?今のところ、レスポンスは、通信しているhtml/phpファイル全体です。 – thebiglebowski11

+0

また、私のdidReceiveData方法は、仕上げされていないようです...それはケースになるだろう、なぜ任意のアイデア? – thebiglebowski11

+0

それ以外に何がありますか? ; ' – jonkroll

0

- NSUTF8StringEncodingを使用してみてください - あなたはNSDataオブジェクトに変換して、それを失う可能性があります。また、あなたがそうでなければ、あなたのパスワードをHTTPSを使用する必要があります

は:-)

関連する問題