objective-c - 使用 PHP/MySQL 和 HTTP 响应验证 Iphone 登录

标签 objective-c authentication

我正在尝试使用 PHP/MySQL 创建一个 iPhone Objective C 登录页面来进行身份验证。我尝试使用 HTTP 响应作为身份验证的方式。现在无论你输入什么,总是无法登录。

`- (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 中是否正确格式化了我的回复。

提前致谢。

最佳答案

感谢您的回复。我使用以下方法让它工作:

- (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;
}

关于objective-c - 使用 PHP/MySQL 和 HTTP 响应验证 Iphone 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2407973/

相关文章:

iOS/Soundcloud API - 从 Soundcloud API 下载轨道,无需 stream_url 请求

objective-c - SKShapeNode定位

php - PHP 哈希函数(包括盐和原始输出)的 Node.js 等价物是什么?

java - 表单验证成功后出现 403

MongoDB认证访问本地数据库

objective-c - 如何让 NSTextView 具有不同的字体大小仅用于打印?

objective-c - 是否可以将 "keyed-subscripting"添加到 Class 对象?

ios - 无法在实现中引用委托(delegate)协议(protocol)

tensorflow - 有人找到了使 Tensorboard 受密码保护的方法吗?

python - 使用 HTTP Digest 身份验证保护 REST 端点