0
嗨我正在嘗試爲應用程序創建一個簡單的登錄頁面。登錄將在本地應用程序上完成,而不是在網絡上完成。目前模擬器運行時沒有錯誤,但同時沒有進行認證。我可以知道如何實現身份驗證?應用程序登錄功能問題
我的代碼:
- (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);
?>
你是說**的NSLog(@ 「成功」,returnString); **得到(空)...? –