2013-03-26 18 views
0

我在iphone中使用webservice進行編程,它訪問包含「Mobile & gaming solutions」等值的mysql數據庫。我從用戶的字符串中獲取這個值並傳遞給php文件。但PHP不接受'&'的字符串。它只接受「移動」。幫我。我在Xcode代碼:從xcode到php的字符串中傳遞'&'?

strEditbuunit = [labeditbunit.text stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 

NSString *Updpost =[NSString stringWithFormat:@"buname=%@",strEditbuunit]; 

UpdLTRhostStr = @"http://localhost/practice/ltrupdate.php?"; 

UpdLTRhostStr = [UpdLTRhostStr stringByAppendingString:Updpost]; 
UpdLTRdataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:UpdLTRhostStr]]; 
UpdLTRserverOutput = [[NSString alloc] initWithData:UpdLTRdataURL encoding: NSASCIIStringEncoding]; 


<?php 
$con = mysql_connect("192.168.0.82","android","android"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db("androidlogin", $con); 
$buname = $_GET["buname"]; 
$retval1 = mysql_query("SELECT code_id FROM typedescs where name = '$buname'"); 
    while($row1 = mysql_fetch_assoc($retval1)) 
    { 
     $bu_id = $row1['code_id']; 

    } 
echo $buname; 
mysql_close($con); 
?> 

當我回聲buname它給了我「移動」而不是因「&」「移動&遊戲解決方案。」請幫助我如何將特殊字符的字符串傳遞給xcode中的php。

+3

不錯[SQL注入漏洞](http://bobby-tables.com)。享受你的服務器pwn3d。 – 2013-03-26 05:31:27

回答

1

它與PHP無關。您沒有在您的網址中引用查詢參數。

UpdLTRhostStr = @"http://localhost/practice/ltrupdate.php?"; 
UpdLTRhostStr = [UpdLTRhostStr stringByAppendingString:Updpost]; 

下面是做這件事:

UpdLTRhostStr = [NSString 
    stringWithFormat:@"http://localhost/practice/ltrupdate.php?buname=%@", 
    CFURLCreateStringByAddingPercentEscapes(
     kCFAllocatorDefault, (CFStringRef) buunit, NULL, 
     CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8)]; 

不要忘記,你需要逃避在PHP端的字符串,也將它傳遞到MySQL之前。正如馬克B指出的,你有一個嚴重的安全漏洞。

+0

如何做到這一點。請給我一些例子。 – VadivuPandi 2013-03-26 05:40:15

+0

@ user2210195:例如,如果您有'&',它將被替換爲'%26'。其他特殊字符以相同的方式被替換。 – 2013-03-26 06:38:50

+0

我用$ buname = mysql_escape_string($ _ GET [「buname」]);在PHP方面。它是正確的。它不工作。我新來php也.plz幫助 – VadivuPandi 2013-03-26 07:33:01