使用PHP/MySQL和HTTP响应对Iphone登录进行身份验证 [英] Authenticating Iphone Login with PHP/MySQL and HTTP Responses

查看:140
本文介绍了使用PHP/MySQL和HTTP响应对Iphone登录进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP/MySQL进行身份验证以创建iPhone Objective C登录页面.我正在尝试使用HTTP响应作为身份验证的方式.现在,无论您输入什么,它始终无法登录.

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];
  }
`

PHP看起来像

    <?
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);


?>

有人知道吗?即使只是告诉我即时消息是否在PHP中正确格式化了我的回复.

Anyone got any pointers? Even if its just to tell me if Im formatting my repsonses correctly in the PHP.

提前谢谢.

推荐答案

感谢您的答复.我使用它来工作:

Thanks for the response. I got it to work using:

- (IBAction) login: (id) sender
{

NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text];

NSString *hostStr = @"MY URL.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];    
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:@"Yes"]){
    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"success" message:@"You are authorized"
                                                          delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alertsuccess show];
    [alertsuccess release];

} else {
    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Invalid Access"
                                                          delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alertsuccess show];
    [alertsuccess release];

}

loginIndicator.hidden = FALSE;
[loginIndicator startAnimating];
loginButton.enabled = FALSE;
}

这篇关于使用PHP/MySQL和HTTP响应对Iphone登录进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆