Hi there, Im trying to create a iPhone Objective C login page using PHP/MySQL to authenticate. Im trying to use HTTP responses as a way of authenticating. Right now it always fails to login , regardless of what you enter.
`- (IBAction) login: (id) sender { // this part isnt really implemented yet! NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text];
NSURL *url = [NSURL URLWithString:@"MY URL HERE.php"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)request; responseStatusCode = [httpResponse statusCode];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:response error:nil];
if ([responseStatusCode statusCode] == 200) {
//do something
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"success" message:@"Your have logged in" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alertsuccess show];
[alertsuccess release];
}
else {
NSLog(@"Login failed.");
//do something else
UIAlertView *alertfail = [[UIAlertView alloc] initWithTitle:@"fail" message:@"login fail" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alertfail show];
[alertfail release];
}
`
and the PHP looks like
<?
session_start(); require("iphoneconnection.php");
$u = $_POST['username']; $pw = $_POST['password'];
$check = "SELECT username, password FROM iphoneusers WHERE username='$u' AND password= '$pw'";
$login = mysql_query($check, $connect) or die(mysql_error());
// check user level and store in $row
if (mysql_num_rows($login) == 1) { $row = mysql_fetch_assoc($login); //$_SESSION['level'] = $row['level']; $_SESSION['username'] = $u; echo 'login success'; header("HTTP/1.1 200 OK");
//header("Location: index.php");
} else { //header("Location: login.php"); //echo '403 Forbidden'; header("403 Forbidden"); }
mysql_close($connect);
?>
Anyone got any pointers? Even if its just to tell me if Im formatting my repsonses correctly in the PHP.
Thanks in advance.