我想從xcode發送用戶名和密碼到我已經創建的php文件。由於我對xcode和php都是新手,爲了簡單應用程序的目的,用戶名和密碼是預定義的「123456」。我有很多麻煩發送用戶名和密碼到PHP網站,並收到回覆。當我的代碼通信並建立連接時,它不會讀取php文件中回顯的變量。相反,它會獲得頁面的完整源代碼。我的代碼如下:如何發送和接收來自Xcode(目標c)的數據到php
目標C代碼:
//我已經設置警報視圖,以及設置兩個按鈕(繼續= buttonIndex == 0和取消= buttonIndex == 1)
// The user continues from the alert view
if(buttonIndex==0)
{
NSString* username = @"123456";
NSString* pass = @"123456";
// I combine the two strings to send to the php page, and insert a : between the two strings
NSString* combined = [NSString stringWithFormat:@"%@:%@", username, pass];
NSString *post = combined;
// Now I attempt to post the data to the php page
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://www.sulaimanmajeed.com/login.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData data] retain];
}
else
{
}
}
}
現在,我也有需要被調用執行後,當其他方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
// NSLog(loginStatus);
dataOutput.text = loginStatus;
[loginStatus release];
[connection release];
[webData release];
}
我的PHP代碼是非常簡單的:
<?php
$usernameEntered = $_GET['user']; $passwordEntered = $_GET['pass']; $full = 'Choose';
$username = array(
"123456", "234567", "345678", "456789");
$password = array(
"123456", "234567", "345678", "456789");
$accounts = array(
"Chequings-874576",
"Savings-793756",
"Visa-018375",
"Mastercard-375836");
$b = 0; $elementsInArray = count($accounts);
$isTrue = "FALSE";
if (($usernameEntered== $username[0]) && ($passwordEntered == $password[0])) {
$isTrue = "TRUE"; } elseif ((usernameEntered == $username[1]) && ($passwordEntered == $password[1])) {
$isTrue = "TRUE"; } elseif ((usernameEntered == $username[2]) && ($passwordEntered == $password[2])) {
$isTrue = "TRUE"; } elseif ((usernameEntered == $username[3]) && ($passwordEntered == $password[3])) {
$isTrue = "TRUE"; } else {
$isTrue = "FALSE"; }
if($isTrue == "TRUE") { for ($i=0; $i < $elementsInArray; $i++) { $b = $i; $full = $full . ':' . $accounts[$b] ; }
echo $full;
}
if($isTrue == "FALSE") {
echo "error:error 2:test 1:test 2:test 3";
}
?>
所以,dataOutput.text應該返回一個「真」值,因爲我提供的用戶名和密碼是默認的「123456」。但dataOutput.text只返回網站的完整源代碼。有人能幫助我找出它爲什麼不會輸出真實信息,以及我的代碼出錯的地方嗎?我一直在爲此工作2天,而且我很難過。先謝謝你。
P.S.
我收到DataOutput中的輸出是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!-- HTML generated by Network Solutions Website Builder --><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sulaiman Majeed login</title><meta name="robots" content="index, follow" /><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /><meta name="description" content="Welcome to my website"><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http-equiv="expires" content="-1"/><script type="text/javascript" src="/scripts/user.js"></script><script type="text/javascript" src="/scripts/jquery.js"></script><script type="text/javascript" src="/scripts/simplegallery.js"></script><script type="text/javascript" src="/scripts/photo_album.js"></script><link type="text/css" href="/scripts/PrebuiltBlockStructure.css" media="screen" rel="stylesheet"><link rel="stylesheet" media="screen" href="/scripts/template.css" id="wb-template.css" /><link rel="stylesheet" media="all" href="/scripts/layout3.css" id="wb-layout3.css" /><link rel="stylesheet" media="all" href="/scripts/website.css" id="wb-website.css" /><link rel="stylesheet" media="print" href="/scripts/print.css" id="wb-print.css" /><link rel="stylesheet" media="all" href="/scripts/12custom.css" id="wb-custom.css" /><link rel="stylesheet" media="all" href="/scripts/icwebsiteelement.css" id="wb-icwebsiteelement.css" /><link rel="stylesheet" media="all" href="/share/scripts/PrebuiltBlockStructure.css" id="wb-/share/scripts/PrebuiltBlockStructure.css" /></head><body onload="initClientMenu();sizeColumns();"><!-- Start content section --><div id="wb-container-outer"> <div id="wb-container-inner"> <div id="wb-layout"> <div id="wb-layout-inner"> <div id="wb-layout-top-header"> <div id="wb-navigation-subtop-header"> </div> <div id="wb-top-subbasenav"></div> </div> <div id="wb-header-nav"> <div id="wb-navigation-top-header"> <div id="wb-navigation-bottom-header"> </div> </div> </div> <div id="wb-layout-header"> <div id="wb-layout-header-mask" class="wb_region"> <table id="wb_con__42340" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__45678" class="wb_col" width="50%" valign="top" style=""> </td><td id="wb_col__5673453" class="wb_col" width="50%" valign="top" style=""></td> </tr> </table> </div> </div> <div id="wb-layout-header-base"></div> <div id="wb-layout-top-2"> <div id="wb-navigation-subtop"> <div><a href="home.html" class="tertiary" target="_self"><h3>Home</h3></a></div> <div><a href="resume.html" class="tertiary" target="_self"><h3>Resume</h3></a></div> </div> <div id="wb-top-subbasenav"></div> </div> <div id="wb-container-contentwrapper"> <div id="wb-layout-sidebar"> <div id="wb-navigation"> <div id="wb-navigation-top"> <div id="wb-navigation-bottom"> <div><a href="home.html" class="tertiary" target="_self"><h3>Home</h3></a></div> <div><a href="resume.html" class="tertiary" target="_self"><h3>Resume</h3></a></div> </div> </div> </div> <div id="wb-basenav"></div> <div id="wb-navbasewrap" class="wb_region"> <div class="wb_dropzone_y"> </div> <table id="dz-tablewb_element_text__02773140" class="dz-table" cellspacing="0" cellpadding="0" width="100%"> <tr> <td align="left"> <div id="wb_element_text__02773140" class="ic-element"> <div></div> </div> </td> </tr> </table> </div> </div> <div id="wb-layout-main"> <div id="wb-layout-main-inner-1"> <div id="wb-layout-main-inner-2" class="wb_region wb-text-content"> <table id="wb_cl__12342346" cellpadding="0" cellspacing="0" class="" width="100%"> <tr> <td> <table id="wb_con__056756794678680" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__089563564560" class="wb_col" width="100%" valign="top" style=""> </td> </tr> </table> <table id="wb_con__1231213564678" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__335786456" class="wb_col" width="70%" valign="top" style=""> <table id="dz-tablewb_element_php__1358289049875_15" class="dz-table" cellspacing="0" cellpadding="0" width="100%"> <tr> <td align="left"> <div id="wb_element_php__1358289049875_15" class="ic-element"><php? ; ?></div> </td> </tr> </table> </td><td id="wb_col__234456134" class="wb_col" width="30%" valign="top" style=""> </td> </tr> </table> <table id="wb_con__5675679467868" class="wb_con" cellpadding="0" cellspacing="0" width="100%"> <tr> <td id="wb_col__8956356456" class="wb_col" width="100%" valign="top" style=""> </td> </tr> </table> </td> </tr> </table> </div> </div> </div> <div style="clear: both"></div> </div> <div id="wb-layout-footer"> <div id="wb-layout-footer-inner-1" class="wb_region"> <div class="wb_dropzone_y"> </div> </div> </div> </div> </div> </div> </div><table border="0" width="100%" cellpadding="0" cellspacing="0" id="other_footer"> <tr> <td align="left" valign="middle"><p style="font-size: 10px; font-style: normal; font-family: arial,verdana,sans-serif; font-style: normal;"><a style="text-decoration: none; font-size: 10px; font-style: normal; font-family: arial,verdana,sans-serif; font-style: normal;" title="Web hosting" href="http://ads.networksolutions.com/landing?code=P61C100S1N0B142A1D255E0000V100">Web Hosting</a> powered by Network Solutions®</p></td> </tr> </table> <!-- End content section --><!-- Start nsWebstats code. Copyright 2010 networksolutions.com More info available at http://networksolutions.com --><script type="text/javascript">var pageName="login"; document.write(unescape('%3Cscript src="'+(location.href.split(':')[0] =='https'?'https':'http')+'://stats.netsolads.com/jsconfig?pid=288656091%26mg=all%26lc=1%26pe=1"%3E%3C/script%3E'));</script><!--End nsWebStats tracking code. --></body></html>
如果您粘貼的是您的PHP代碼,則會出現嚴重的語法問題。您只需簡單地讓腳本無誤地運行就可以開始。 –
他說什麼......用'$ password [0]'而不是'password(0)'和'$ result_array [0]'而不是'$ result_array(0)'....'和'$ username [ 0]'而不是'username(0)'等等。 – glomad
仍然會得到一些[語法錯誤](http://codepad.viper-7.com/ONSEkI),請先調試一下。 並且一個php代碼以'<?php'開頭而不是'
Xavier