您好我正在尝试为应用程序创建一个简单的登录页面。登录将在本机应用程序上完成,而不是在Web上完成。目前,运行模拟器时没有错误,但同时没有进行身份验证。我可以知道如何实现身份验证吗?
我的代码:
- (IBAction) login: (id) sender
{
NSString *content = [NSString stringWithFormat:@"Agent_code=%@&Password=%@",[Agent_code text], [Password text]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://test/login.php?"]];
[request setHTTPMethod:@"GET"];
[request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];
// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"success",returnString);
}
PHP
<?php
# Init the MySQL Connection
$username = "user";
$dbname = "db";
$password = "pass";
$hostname = "localhost";
$table = "table";
if (!($connectDB = mysql_connect($hostname, $username, $password)))
die ('Failed to connect to MySQL Database Server - #'.mysql_errno().': '.mysql_error());
if (!mysql_select_db ($dbname))
die ('Connected to Server, but Failed to Connect to Database - #'.mysql_errno().': '.mysql_error());
# GETTING PARAMETERS
$Agent_code = $_GET['Agent_code'];
$Password = $_GET['Password'];
# Prepare the SELECT Query
$selectSQL = "SELECT Agent_code, Password FROM $table WHERE Agent_code = '$Agent_code' AND Password = '$Password'";
# Execute the SELECT Query
if (!($selectRes = mysql_query ($selectSQL))) {
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
} else {
$row = mysql_fetch_assoc ($selectRes);
if (mysql_num_rows ($selectRes) == 1) {
echo 'User and password found!';
} else {
echo 'Invalid Username or Password.';
}
}
mysql_close($connectDB);
?>
答案 0 :(得分:0)
确定。这些可能是问题:
NSLog(@"success:%@",returnString);
请尝试此解决方案:
- (IBAction)login:(id)iSender {
NSString *aContent = [NSString stringWithFormat:@"Agent_code=%@&Password=%@",[Agent_code text], [Password text]];
NSString *anURLString = [NSString stringWithFormat:@"http://test/login.php?%@", aContent];
NSURL *anURL = [NSURL URLWithString:anURLString];
NSError *anError = nil;
NSURLResponse *anURLResponse;
NSURLRequest *anURLRequest=[NSURLRequest requestWithURL:anURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSData *aResponseData = [NSURLConnection sendSynchronousRequest:anURLRequest returningResponse:&anURLResponse error:&anError];
NSString *aResponse = [[NSString alloc] initWithData:aResponseData encoding:NSUTF8StringEncoding];
NSLog(@"RESPONSE = %@",aResponse);
}