php - 尝试通过将用户名/密码发送到 PHP 脚本来验证 iOS 中的用户身份

标签 php ios

我试图通过从我的 iOS 应用程序向 php 脚本发送 POST 请求来验证用户身份。然后让 iOS 应用程序读取 NSHTTPURLResponse 以确定它是否成功。我的 iOS 代码如下所示,由于某种原因,它说我的 NSURLConnection 未使用。 int 代码总是返回值 200。

- (void)authenticateUser:(NSString *)aUsername
                password:(NSString *)aPassword
{
    NSString *post = [NSString stringWithFormat:@"username=%@&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];
    }
}

此外,如果您想查看我的 php 脚本,我在虚拟变量中输入了该脚本并进行了测试,看看它是否正确查询了数据库并检查了...但以防万一您好奇。

<?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 && $selectedUsername == $sentUsername) {
            $authenticate = 1;
        }
    }
    echo $authenticate;
?>

我尝试设置 PHP header ,但我不确定如何执行此操作。我感谢这个社区给我的所有帮助,这真的非常宝贵。谢谢!

最佳答案

您返回的“200”代码是 HTTP 状态代码,表示请求成功。

如果您想要从 PHP 脚本发回“1”或“0”值,则应该在 didReceiveData: 委托(delegate)方法中查找它,而不是 didReceiveResponse: .

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    NSLog(@"authenticate value => %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);
}

关于php - 尝试通过将用户名/密码发送到 PHP 脚本来验证 iOS 中的用户身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407804/

相关文章:

ios - swift iOS : UIPageController inside UINavigation controller

ios - 带有文本和图像的 UIBarButtonItem - iOS 8

php - 使用 PHP 的 Lucene

php - sphinx 全文搜索与列过滤

php - 如何让 ZipArchive 不覆盖某些文件和文件夹

objective-c - 改变正在进行的 CAKeyframeAnimation 动画的速度

ios - 录像没有声音?

php - 有人能帮我弄清楚为什么这个邮政编码类找不到范围内的邮政编码吗?

php - 优化以下 PHP 代码的最佳方法?

ios - Unwind Segues 不适用于 Xcode 6