ios user registration fails when double pressed

Sunday, July 6, 2014

I am attempting a member registration in ios using php script to communicate with localhost mysql. Insertion and retrieval works well. I am trying now to protect against users with same email being able to register. This is what I did:



<?php
header('Content-type: text/plain; charset=utf-8');


$db_conn = new PDO('mysql:host=localhost;dbname=pal','root','root');
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$message = "";
$email = ($_POST['email']);

$qry = $db_conn->prepare('SELECT email FROM users WHERE email = :email');
$qry->bindParam(':email', $email);
$qry->execute();
$result = $qry->fetchColumn();

//return $result;

echo utf8_encode($result);
?>


and in my viewcontroller, I did this:



-(IBAction)registrationBtn:(id)sender{
NSString *email = self.emailField.text;
NSString *password = self.passwordField.text;
NSString *firstName = self.firstNameField.text;
NSString *lastName = self.lastNameField.text;
NSString *birthYear = self.yearField.text;
/*String will contain Male/Femald */
NSString *gender = [self.genderField titleForSegmentAtIndex:self.genderField.selectedSegmentIndex];

if(email.length > 0 && password.length > 0 && firstName.length > 0 && lastName.length > 0 && birthYear.length > 0 && gender.length > 0)
{

BOOL userExist = [self checkIfUserExists:email];
NSLog(@"Boolean Results:%hhd", userExist);
if(userExist == false)
{
NSLog(@"User Doesn't Exist..Proceeding with Registration");
/*All fields checked not empty, create raw string for HTTP*/
NSString *rawStr = [NSString stringWithFormat:@"email=%@&password=%@&&firstname=%@&lastname=%@&birthyear=%@&gender=%@", email,
password,
firstName,
lastName,
birthYear,gender];

NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/registration.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];


NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"Response String: %@", responseString);

NSString *success = @"success";
[success dataUsingEncoding:NSUTF8StringEncoding];

[self dismissViewControllerAnimated:YES completion:nil]; // Dismiss the viewController upon success
}else{
/*Issue error that user exists with this email*/
NSLog(@"%s", "Error");
}

}

}


and the function being called checkIfUserExist is here too:



-(BOOL)checkIfUserExists:(NSString*)userEmail
{
NSString *rawStr = [NSString stringWithFormat:@"email=%@", userEmail];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/checkUserExist.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];


NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];

NSString *success = @"success";
[success dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Check User Reponse String: %@", responseString);
if([responseString isEqualToString:userEmail])
{
NSLog(@"Check User = True");
return true;
}else{
NSLog(@"Check User = False");
return false;
}
}


I start the simulator with database table empty. I fill information in registration form and click button. Data is stored in database. Next time I click the button register again, it says it exists so can't create record (correct behaviour). Third time or so many number of click afterwards, it keeps adding and saying user doesn't exist.


Please help.







http://ift.tt/1obqkjL