目前在做用户登录,需要把用户名和密码发到服务器验证,服务器端用PHP接受,测试了好久,就是不能发送,我把全部代码都贴出来了,大家看看啦。头文件
#import <UIKit/UIKit.h>@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;
} ?>
#import <UIKit/UIKit.h>@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;
} ?>
解决方案 »
- pthread在等待线程结束时,怎么样不影鼠标键盘等事件的响应
- 关于NSGenericException-使用NSEnumerator
- 急求用Objective-C编写的计算器代码,高手请进......
- java程序员想学iphone开发容易吗?
- 请教各位前辈们,IPAD2的软件快捷图标是多大的呀?
- 求助3个功能的实现(求越狱开发高手帮忙)
- 国内IOS,一般都开发些什么?
- iOS关于webservice接口调用的问题,在线等,急急
- NSDecimal 保留2位小数
- uitableView 利用SDWebImage 缓存之后,图片不显示,滚动刷新之后就显示了,怎么回事
- 关于不能socket不能和服务器通讯问题
- Iphone上的web开发,怎么实现文件下载?
给分吧~
@"username=%@&password=%@"
改成
@"?username=%@&password=%@"