gpt4 book ai didi

Iphone sdk,如何将数据发布到网络服务器?有人有例子吗?

转载 作者:行者123 更新时间:2023-12-03 21:11:36 24 4
gpt4 key购买 nike

伙计们,我想将数据发布到网络服务器,例如用户名和密码,在网络服务器上,我使用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];
}

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

?>

最佳答案

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

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

应该看起来更像这样:

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

因为您实际上并未发布到服务器,所以您只是使用 GET 请求和 URL 参数。

我不知道您是否计划在您的设备上后台运行 Web 服务器和 PHP,但在某些时候我假设您会更改 http://localhost更改为其他内容,希望您在让人们导航到您的网页并在 URL 查询中输入他们想要的任何内容之前先清理您的 SQL 查询。

关于Iphone sdk,如何将数据发布到网络服务器?有人有例子吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com