2014-10-05 40 views
0

我正在使用php來讓我的xcode應用程序讀取和寫入數據到我的mysql數據庫。這是我的代碼獲取Php和mysql數據庫進行通信

<?php 
if (isset($_GET["userName"]) && isset($_GET["password"])){ 
      $userName = $_GET["userName"]; 
      $password = $_GET["password"]; 
      $result = login($userName, $password); 
      echo $result; 
      } 

function makeSqlConnection() 
{ 
$DB_HostName = "what do i put here?"; 
$DB_Name = "i know this"; 
$DB_User = "and this"; 
$DB_Pass = "And this"; 

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 

    mysql_select_db($DB_Name,$con) or die(mysql_error()); 

return $con; 
} 

function disconnectSqlConnection($con) 
{ 
mysql_close($con); 
} 

function login($userName, $password) 
{ 

$con = makeSqlConnection(); 

$sql = "select * from user where userName = '$userName' and password = '$password';"; 
$res = mysql_query($sql,$con) or die(mysql_error()); 

$res1 = mysql_num_rows($res); 

disconnectSqlConnection($con); 

if ($res1 != 0) { 
    return 1; 
}else{ 
    return 0; 
} 


} 

?> 

另外我要做些什麼才能保證安全。感謝您給予的任何幫助。 另外我運行這個我的vps。

傢伙在Xcode後我使用這個代碼的數據:

- (IBAction)continueClicked:(id)sender { 
NSInteger success = 0; 
@try { 

    if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""]) { 

     [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0]; 

    } else { 
     NSString *post =[[NSString alloc] initWithFormat:@"userName=%@&passWord=%@",[self.txtUsername text],[self.txtPassword text]]; 
     NSLog(@"PostData: %@",post); 

     NSURL *url=[NSURL URLWithString:@"(my php url)"]; 

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

     NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 

     //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

     NSError *error = [[NSError alloc] init]; 
     NSHTTPURLResponse *response = nil; 
     NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

     NSLog(@"Response code: %ld", (long)[response statusCode]); 

     if ([response statusCode] >= 200 && [response statusCode] < 300) 
     { 
      NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
      NSLog(@"Response ==> %@", responseData); 

      NSError *error = nil; 
      NSDictionary *jsonData = [NSJSONSerialization 
             JSONObjectWithData:urlData 
             options:NSJSONReadingMutableContainers 
             error:&error]; 

      success = [jsonData[@"success"] integerValue]; 
      NSLog(@"Success: %ld",(long)success); 

      if(success == 1) 
      { 
       NSLog(@"Login SUCCESS"); 
      } else { 

       NSString *error_msg = (NSString *) jsonData[@"error_message"]; 
       [self alertStatus:error_msg :@"Sign in Failed!" :0]; 
      } 

     } else { 
      //if (error) NSLog(@"Error: %@", error); 
      [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0]; 
     } 
    } 
} 
@catch (NSException * e) { 
    NSLog(@"Exception: %@", e); 
    [self alertStatus:@"Sign in Failed." :@"Error!" :0]; 
} 
if (success) { 
    [self performSegueWithIdentifier:@"login_success" sender:self]; 
} 
} 

不想騙我發現這個代碼eldewhere,我試圖得到它的工作,所以我可以做一個登錄頁面。

+0

我認爲它是特別是導致問題的主機名部分。我把本地主機還是? – Tavis 2014-10-05 09:14:59

+0

主機名取決於您的實際設置。 'localhost'在很多情況下都可以使用,請嘗試一下。爲了使它更安全,不要使用字符串連接來建立你的sql查詢,而是使用預處理語句。 – FAS 2014-10-05 09:17:35

+2

而mysql_ *函數已被棄用,請使用mysqli_ *或PDO。 – FAS 2014-10-05 09:18:59

回答

0

我相信這個問題是在你的連接:

function makeSqlConnection() 
{ 
    $DB_HostName = "localhost"; 
    $DB_User = "your_database_user"; 
    $DB_Pass = "your_database_user_password"; 
    $DB_Name = "your_database_name"; 

    $con = mysql_connect($DB_HostName,$DB_User,$DB_Pass, $DB_Name) or die(mysql_error()); 

    return $con; 
} 
+0

請看我編輯的問題 – Tavis 2014-10-05 10:21:23

0

$ DB_HostName應該是您的主機名/服務器的IP地址和端口。例如,假設你有一臺IP地址爲162.2.2.1的服務器,並且你服務器上的MySql端口是(如果我沒有弄錯,默認的mysql端口) - 你現在應該寫$ DB_HostName = '162.2.2.1:3306'就是這樣! 如果你有一個主機名代替,db.php-db.com你可以寫,而不是IP地址: $ DB_HostName = 「db.php-db.com:3306」

關於安全問題:您可以使用SSL加密,使您的連接加密,在這裏閱讀更多:https://serverfault.com/questions/261134/is-mysql-port-3306-encrypted-and-if-no-how-can-i-encrypt-it

祝你好運, 湯姆。

+0

請看看我編輯過的問題 – Tavis 2014-10-05 10:20:29