2010-05-27 64 views
2

伙计们,我想在Web服务器上发布数据到Web服务器,如用户名和密码,我使用PHP来回应是或否,附上所有代码。任何人都可以帮我解决问题,代码有什么问题。因为它总是说不正确的密码或用户名。我试图用里面的用户名和密码来测试php代码,它正在工作。所以请帮助我。谢谢。Iphone sdk,如何发布数据到网络服务器?有人有例子吗?

头文件

@interface kiksyloginViewController : UIViewController { 
    IBOutlet UITextField *usernameField; 
    IBOutlet UITextField *passwordField; 
    IBOutlet UIButton *loginButton; 
} 

@property (nonatomic, retain) UITextField *usernameField; 
@property (nonatomic, retain) UITextField *passwordField; 
@property (nonatomic, retain) UIButton *loginButton; 

- (IBAction) login: (id) sender; 

@end 

实现文件

#import "kiksyloginViewController.h" 

@implementation kiksyloginViewController 

@synthesize usernameField; 
@synthesize passwordField; 
@synthesize loginButton; 

- (IBAction) login: (id) sender 
{ 

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

    NSString *hostStr = @"http://localhost/userlogin.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:@"Congrats" message:@"You are authorized " 
                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 

     [alertsuccess show]; 
     [alertsuccess release]; 

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

    } 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn’t have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren’t in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

PHP文件

<?php 
//$Container = new Container; 

$u = $_GET['username']; 
$pw =$_GET['password']; 
$check = "select username, password from user where username='$u' and password='$pw'"; 
function myDbconn() 

{ 
    mysql_connect("localhost", "root","") or die(); 
    mysql_select_db("test") or die (mysql_error()); 
} 

myDbconn(); 


$login = mysql_query($check) or die (mysql_error()); 
//$run = DB()->select($check); 



if (mysql_num_rows($login)==1){ 
    //$row = DB()->fetch($run); 
    //$row = DB()->fetch($login); 
    $row = mysql_fetch_assoc($login); 
    echo 'yes'; 
    exit;   
} 
else { 
    echo 'No'; 
    exit; 
} 

?> 

回答

1

第一个问题,我看到这可能是最大的,有没有? “”任何地方。具体做法是:

@"username=%@&password=%@" 

应该看起来更像是这样的:

@"?username=%@&password=%@" 

因为你没有真正发送到服务器,你仅仅使用GET请求和URL参数。

我不知道你是否打算在后台运行你的设备上的web服务器和PHP,但是在某些时候我假设你会将http://localhost更改为其他东西,并且希望你在之前会对SQL查询进行清理您可以让用户浏览您的网页并在网址查询中输入他们想要的任何内容。

1

的职位是旧的,但无论如何,如果它可以帮助别人:

呀,它有一对夫妇的错误。首先,在文件的.m正确的字符串应该是:

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

此外,在文件的.m:

if([serverOutput isEqualToString:@"yes"]){ 

,而不是 “是”。注意大写字母!

这就是全部。代码运行良好。