5 Replies Latest reply on Jan 3, 2009 10:58 AM by rtalton

    Flex communication with PHP

    SmilingRoses Level 1
      I have a simple username and password panel with a login button.
      I created an HTTPSerivce to call a login.php file that connects to a mySql database.
      Testing the php without Flex works fine. If the username and password have a match in the database the output is yes or no depending on the number of rows resulting from the query.

      I created a loginResultHandler in the script section of Flex to handle the results of the php call. Right now all it does is show an Alert message that says Login Success or Login Failure depending on the results of the php call.

      However, the message always says Login Failure even if I know the login information is correct and works when testing without Flex. Now here is a weird aspect of this whole thing. I tried lots of different things to fix this problem and here is another scenario.

      The above error happens when the php code says if($rowCheck > 0){$output .= "yes";}else{$output .= "no";}

      However if the php code says if(!$result){$output .= "no";}else{$output .= "yes";} then the Alert message comes back with Login Success even when I know the login information is incorrect.

      I've encluded the code below in case someone wants to help me out with this.

      FLEX CODE FOR LOGIN PANEL

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

      <mx:Script>
      <![CDATA[
      import mx.rpc.events.ResultEvent;
      import mx.controls.Alert;

      private function loginResult(event:ResultEvent):void
      {
      if(loginService.lastResult.loginsuccess == "yes")
      {
      Alert.show("Login Success");

      }
      else//if(loginService.lastResult.loginsuccess == "no")
      {
      Alert.show("Incorrect login information", "Login Failed");
      }
      }
      ]]>
      </mx:Script>

      <mx:HTTPService id="loginService"
      url="login.php"
      useProxy="false"
      method="POST"
      result="loginResult(event)">
      <mx:request xmlns="">
      <email>{loginEmail.text}</email>
      <pass>{loginPass.text}</pass>
      </mx:request>
      </mx:HTTPService>

      <mx:Panel width="335" height="170" layout="absolute" title="Login" right="167" top="28">
      <mx:Form x="10" y="10">
      <mx:FormItem label="Email:">
      <mx:TextInput id="loginEmail" maxChars="75"/>
      </mx:FormItem>
      <mx:FormItem label="Password:">
      <mx:TextInput id="loginPass" displayAsPassword="true" maxChars="30"/>
      </mx:FormItem>
      </mx:Form>
      <mx:ControlBar>
      <mx:Button label="Login" id="loginBtn" click="loginService.send()" />
      </mx:ControlBar>
      </mx:Panel>
      </mx:Application>

      PHP CODE

      <?php

      $host="localhost";
      $user="XXXX";
      $pass="XXXX";
      $db="XXXX";

      $con = mysql_connect($host,$user,$pass) or die("No Connection ");
      mysql_select_db($db,$con);

      $email = $_POST["loginEmail"];
      $pass = $_POST["loginPass"];

      //$email = "jane@doe.com"; // I use this for testing in the browser without Flex
      //$pass = "jane";

      $query = "select * from bclients where email='$email' and pass='$pass'";
      $result = mysql_query($query);

      $rowCheck = mysql_num_rows($result);

      $output = "<loginsuccess>";

      if(!$result)
      {
      $output .= "no";
      }
      else
      {
      $output .= "yes";
      }
      $output .= "</loginsuccess>";

      print($output);
      //print($rowCheck); //Also use this for testing in browser

      ?>

        • 1. Re: Flex communication with PHP
          flexstone Level 1
          I believe $result will always be true - there is always "mysql_query($query)";

          what if you change
          if(!$result)....

          to

          if($rowCheck > 0).....
          • 2. Re: Flex communication with PHP
            SmilingRoses Level 1
            I have tried that and the result is always Login Failure even when I know the information is correct and using the same information when testing in the browser without Flex it works.
            • 3. Re: Flex communication with PHP
              Level 7

              "SmilingRoses" <webforumsuser@macromedia.com> wrote in message
              news:gjo5tn$qlv$1@forums.macromedia.com...
              >I have tried that and the result is always Login Failure even when I know
              >the information is correct and using the same information when testing in
              >the browser without Flex it works.

              Try backing up a step and having PHP echo back to Flex what it got from
              Flex. It seems to me that PHP isn't receiving the same information from
              Flex as it is from HTML, even if you think you're sending the same
              information.


              • 4. Re: Flex communication with PHP
                flexstone Level 1
                I always find it helpful to set the resultFormat to "text" while debugging - this way you get the literal results from PHP. This should help isolate the issue.
                • 5. Re: Flex communication with PHP
                  rtalton Level 4
                  I had issues using PHP's mysql_num_rows, but I don't remember why.
                  Instead, I started using mysql_fetch_array.
                  The mysql_fetch_array function will either get you an array of the results, or will return FALSE if there are no results. I use this for PHP logins and have had great success with it.

                  More info here:
                  http://www.php.net/mysql_fetch_array

                  Here's the code I've used with no problems:
                  //Query the database to see if the given username/password combination is valid.
                  $query = "SELECT * FROM tblAccounts WHERE LOG_NAME = '$username' AND LOG_PWD = '$password'";
                  $result = mysql_fetch_array(mysql_query($query));

                  //Begin formatting query success/failure as XML:
                  $output = "<loginsuccess>";
                  //if the query returned results, then output "<loginsuccess>true</loginsuccess>",
                  //else output "<loginsuccess>false</loginsuccess>".

                  //Check for FALSE value. If so, there were no results.
                  if(!$result)
                  {$output .= "false";
                  }else{
                  $output .= "true";
                  }
                  $output .= "</loginsuccess>";