Note that there are some explanatory texts on larger screens.

plurals
  1. POtrying to authenticate users in iOS by sending username/pw to a PHP script
    primarykey
    data
    text
    <p>I am trying to authenticate a user by sending a POST request from my iOS app to a php script. Then having the iOS app read the NSHTTPURLResponse to determine whether or not it was successful. My iOS code is shown below, for some reason it says that my NSURLConnection is unused. The int code always shoots back a value of 200.</p> <pre><code>- (void)authenticateUser:(NSString *)aUsername password:(NSString *)aPassword { NSString *post = [NSString stringWithFormat:@"username=%@&amp;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]; } } </code></pre> <p>Additionally, if you want to take a look at my php script, which I entered in dummy variables for and tested to see if it queried the database properly and that checked out... But just in case you're curious.</p> <pre><code>&lt;?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 &amp;&amp; $selectedUsername == $sentUsername) { $authenticate = 1; } } echo $authenticate; ?&gt; </code></pre> <p>I tried to set the PHP header, but I was unsure how to do that. I appreciate any and all help this community has given me, truly invaluable. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload