伙计们,我想将用户名和密码等数据发布到web服务器上,在web服务器上,我使用PHP回显是或否,我附上了所有的代码。有人能帮我吗,代码出了什么问题。因为它总是显示错误的密码或用户名。我试着用用户名和密码测试php代码,它是工作的。所以请帮帮我。谢谢。
头文件
@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];
}
@endPHP文件
<?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;
}
?>发布于 2010-05-28 06:00:48
我看到的第一个问题可能是最大的问题是没有"?“任何地方。具体地说,这是:
@"username=%@&password=%@"应该看起来更像这样:
@"?username=%@&password=%@"因为您实际上并没有发送到服务器,所以只使用了一个GET请求和URL参数。
我不知道你是否计划让网络服务器和PHP在后台运行在你的设备上,但在某个时刻,我假设你会将http://localhost改成其他的东西,希望在你让人们导航到你的网页并在URL查询中输入他们想要的任何东西之前,你会清理你的SQL查询。
发布于 2013-04-22 23:04:38
这篇文章很老了,但不管怎样,如果它可以帮助其他人:
是的,它有几个错误。首先,文件.m中的正确字符串应该是:
NSString *post =[NSString stringWithFormat:@"?&username=%@&password=%@",
usernameField.text, passwordField.text];同样在文件.m中:
if([serverOutput isEqualToString:@"yes"]){而不是“是”。注意大写字母!
这就是全部。代码运行得很好。
https://stackoverflow.com/questions/2925088
复制相似问题