2010-03-09 58 views
2

我試圖創建一個使用PHP/MySQL進行身份驗證的iPhone目標C登錄頁面。我試圖使用HTTP響應作爲驗證方式。無論你輸入什麼內容,它現在總是無法登錄。使用PHP/MySQL和HTTP響應身份驗證Iphone登錄

`- (IBAction) login: (id) sender 
{ 
// this part isnt really implemented yet! 
NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text]; 




NSURL *url = [NSURL URLWithString:@"MY URL HERE.php"]; 


NSURLRequest *request = [NSURLRequest requestWithURL: url]; 

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)request; 
responseStatusCode = [httpResponse statusCode]; 


NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:response error:nil]; 




if ([responseStatusCode statusCode] == 200) { 
    //do something 
    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"success" message:@"Your have logged in" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alertsuccess show]; 
    [alertsuccess release]; 
    } 

    else { 
    NSLog(@"Login failed."); 
    //do something else 
    UIAlertView *alertfail = [[UIAlertView alloc] initWithTitle:@"fail" message:@"login fail" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alertfail show]; 
    [alertfail release]; 
    } 
` 

和PHP看起來像

<? 
session_start(); 
require("iphoneconnection.php"); 

$u = $_POST['username']; 
$pw = $_POST['password']; 

$check = "SELECT username, password FROM iphoneusers WHERE username='$u' AND password= '$pw'"; 



$login = mysql_query($check, $connect) or die(mysql_error()); 


// check user level and store in $row 

if (mysql_num_rows($login) == 1) { 
$row = mysql_fetch_assoc($login); 
//$_SESSION['level'] = $row['level']; 
$_SESSION['username'] = $u; 
echo 'login success'; 
header("HTTP/1.1 200 OK"); 

//header("Location: index.php"); 

} else { 
//header("Location: login.php"); 
//echo '403 Forbidden'; 
header("403 Forbidden"); 
} 

mysql_close($connect); 


?> 

任何人有任何指針?即使它只是告訴我是否在PHP中正確地格式化我的repson。

在此先感謝。

回答

3

感謝您的回覆。我得到它的工作使用:

- (IBAction) login: (id) sender 
{ 

NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text]; 

NSString *hostStr = @"MY URL.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:@"success" message:@"You are authorized" 
                  delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alertsuccess show]; 
    [alertsuccess release]; 

} else { 
    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Invalid Access" 
                  delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alertsuccess show]; 
    [alertsuccess release]; 

} 

loginIndicator.hidden = FALSE; 
[loginIndicator startAnimating]; 
loginButton.enabled = FALSE; 
} 
1

根據您提供的示例,我沒有看到您實際上發送了請求的用戶名密碼&。您正在創建一個字符串「發佈」,但從未將其包含在請求中。

此外,該投:

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)request;

看起來不正確的。爲什麼要將請求對象轉換爲響應?