0

我已经尝试了很多次来让它工作,但无论我尝试什么都不起作用。我研究了许多教程,但没有找到任何有用的东西。请问有什么建议或帮助吗?!!代码似乎有什么问题吗?

- (IBAction)login:(id)sender {
    NSString *post =[NSString stringWithFormat:@"username=%@&pass=%@",usernameField.text, passwordField.text];

NSString *hostStr = @"http://new-host-3.home/login.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 *alertYes = [[UIAlertView alloc] initWithTitle:@"Succsess!" message:@"You are logged in!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertYes show];
}

else {
    UIAlertView *alertFail = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect"
                                                          delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertFail show];

}

}


<?php
session_start();
$con = mysqli_connect("localhost","root","monkeys","lian");

$u = $_GET['username'];
$pw = $_GET['pass'];

$check = "SELECT username,pass FROM members WHERE username='$u' AND pass='$pw'";

$login = mysqli_query($con,$check) or die(mysqli_error($con));

$num_rows = mysqli_num_rows($login);

echo "$num_rows \n";
echo "$u \n";
echo "$pw \n";

if (mysqli_num_rows($login) >= 1) {
$row = mysqli_fetch_assoc($login);
echo 'Yes'; 
exit;
}

else {
echo ' No';
exit;
}
4

1 回答 1

0

尝试使用 NSMutableURLRequest,并且绝对异步运行它。您的服务可能正在等待登录的 POST。如果是这样,设置 HTTP 动词,并将 post 数据放入请求正文中......

NSString *hostStr = @"http://new-host-3.home/login.php?";
NSURL *url = [NSURL URLWithString:hostStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";

NSString *post =[NSString stringWithFormat:@"username=%@&pass=%@",usernameField.text, passwordField.text];
NSString *postEscaped = [post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *postData = [postEscaped dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

[request setHTTPBody:postData];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           if (!error) {
                               NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"response %@", string);
                           } else {
                               NSLog(@"error %@", error);
                           }
                       }];
于 2013-05-17T04:44:14.563 回答