1

我正在尝试将用户名和密码从 xcode 发送到我制作的 php 文件中。由于我是 xcode 和 php 的新手,并且出于我正在制作的简单应用程序的目的,用户名和密码是预定义的“123456”。我在将用户名和密码发送到 php 站点并收到响应时遇到了很多麻烦。当我的代码进行通信并建立连接时,它不会读取 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 只返回网站的整个源代码。有人可以帮助我找出为什么它不输出 true,以及我的代码哪里出错了吗?我已经为此工作了2天,我很困惑。先感谢您。

附言

我在 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>
4

3 回答 3

0

我确信有更好的方法来做到这一点,但你可以在 UIWebView 中加载 php 文件,然后运行脚本,然后当你想检索变量时,你可以用 php 打印它们,然后使用 javascript 来检索它们和使用其他人在这里提到的方法将变量从javascript传输到objective-c:iOS从UIWebView检索javascript变量

于 2013-01-16T21:13:07.930 回答
0

如果有人对答案感兴趣,那对于一些琐碎的数据发送来说有点矫枉过正,我建议看看 ActiveMQ。您可以设置一个服务器,您的 iOS 设备可以订阅服务器上的队列,以识别其他客户端是否在队列中发送数据。

问候

于 2013-01-16T21:27:36.433 回答
0

您是否使用一些 wordpress 插件或其他可能干扰您的 php 文件的 php 文件运行 php 类?

<script type="text/javascript">var pageName="token"; 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. -->
于 2013-01-16T22:37:44.997 回答