2013-05-16 30 views
0

我已经尝试了很多次,以使其工作,但无论我尝试什么,它都不起作用。我研究了很多教程,但没有找到有用的东西。请提供任何提示或帮助?代码看起来有什么问题吗?iphone登录系统不能正常工作

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

回答

0

尝试使用NSMutableURLRequest,并确实异步运行它。您的服务可能期望登录的POST。如果是,请设置HTTP谓词,并将发布数据放在请求正文中。

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); 
          } 
         }]; 
+0

我试过了,但不管我放在用户名和密码字段中,它都不会返回错误。我只有在输入正确的用户名和密码时才能登录。 – thewhtmba

+0

此代码的职责是制作一个有效的http发布到服务。预期的动词,预期的参数以及发送这些动作时的服务器行为完全取决于Web服务。诊断发生的事情的方法是在此客户端和服务器上输出日志(如果您控制它)。随意在这里粘贴NSLog'ed响应字符串。 – danh

+0

“2013年5月18日22:39:09.603 ServerTest [2294:11303] <!DOCTYPE HTML PUBLIC “ - // IETF // DTD HTML 2.0 // EN”>响应 404未找到

未找到

请求的URL /login.php此服务器上找到。


阿帕奇/ 2.2.22(UNIX)DAV/2 PHP/5.3.15与新宿主3.home了Suhosin贴片服务器端口80
'这是NSLog的输出 – thewhtmba