9 Replies Latest reply on Apr 26, 2010 11:53 AM by Matt Le Fevre

    Flex and PHP Simple Login

    djh88ukwb Level 1

      Hello all,

       

      I wounder if anyone could help?  I am trying to create a very simple log in app.

       

      I want the user to have to enter a username and password and then if correct get directed to a differnt asset.

       

      Now, i know there are loads of examples on the Internet, however they are all a bit complex for what i need.  I am planning on using just one username and password, so there shoudnlt be any need for a database.

       

      I would like it if once the user presses Log In, it looks at a php script and if the values match those that are predefined in the script you get redirected to a new asset.

       

      Is this possible?

        • 1. Re: Flex and PHP Simple Login
          Matt Le Fevre Level 4

          i have all the basic coding here:

           

          http://www.mattlefevre.com/viewExample.php?tut=flexPHP&proj=Simple%20Login%20Application

           

          all you need to do is strip out the database stuff from the login.php script, and do something like this instead.

           

              $username = $_POST["user"];
              $password = $_POST["pass"];
          
              if (($username == "Example Name") && ($password == "Example Password"))
              {
                  echo "<status>true</status>";
              }
              else
              {
                  echo "<status>false</status>";
              }
          
          

           

          if you then look at the loginResult function in the mxml code on that site, you'll see there to call your redirection to a new asset

          • 2. Re: Flex and PHP Simple Login
            djh88ukwb Level 1

            Grizzzzzzzzzz Thanks yet again for your help ! (Y)

             

            I have followed your code as best as i can, but i always get the result Login Failed!

             

            THe code for te flex is as follows -

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" applicationComplete="init()" backgroundGradientColors="[#000000,#FFFFFF]">
            <!--
            ***Coded by Matt Le Fevre***
            For Terms and Conditions please visit
            www.MattLeFevre.com/usage.php
            -->
                <mx:Script>
                <![CDATA[
                    import mx.events.ResizeEvent;
                    import mx.rpc.events.ResultEvent;
                    import mx.controls.Alert;
                   
                    [Bindable]
                    public var username:String;
                    [Bindable]
                    public var password:String;
                    [Bindable]
                    public var newUsername:String;
                    [Bindable]
                    public var newPassword:String;
                    [Bindable]
                    public var dataDump:String = "dataDump";
                   
                    private function tryLogin():void
                    {
                        username = usernameLogin.text;
                        password = passwordLogin.text;
                        usernameLogin.text = "";
                        passwordLogin.text = "";
                        loginService.send();
                    }
                   
                    private function loginResult(evt:ResultEvent):void
                    {
                        if (evt.result.status == true)
                        {
                            Alert.show("Login successful", "Success");
                        }           
                        else
                        {
                            Alert.show("Login failedl", "Failure");
                        }
                    }
                     ]]>
                </mx:Script>
               
               
                <mx:HTTPService id="loginService" url="login.php" method="POST" result="loginResult(event)">
                    <mx:request xmlns="">
                        <user>{username}</user>
                        <pass>{password}</pass>
                    </mx:request>
                </mx:HTTPService>
               
                <mx:Panel title="Try Login" width="249" height="156" layout="absolute">
                    <mx:Label text="password" x="10" y="39"/>
                    <mx:Label text="username" x="10" y="13"/>
                    <mx:TextInput id="usernameLogin" restrict="a-z0-9A-Z" x="72" y="11" width="147"/>
                    <mx:TextInput id="passwordLogin" displayAsPassword="true" restrict="a-z0-9A-Z" width="147" x="72" y="37"/>
                    <mx:Button label="Login" click="tryLogin()" x="86.5" y="84"/>
                </mx:Panel>
              
            </mx:Application>

             

            Then the php file is as you suggested -

             

                $username = $_POST["user"];
                $password = $_POST["pass"];

                if (($username == "admin") && ($password == "pass"))
                {
                    echo "<status>true</status>";
                }
                else
                {
                    echo "<status>false</status>";
                }

             

            Any suggestions as to why it keeps throwing up a failed message?

             

            Thanks again !

            • 3. Re: Flex and PHP Simple Login
              djh88ukwb Level 1

              Sorry ignore my last post, i have since resolved the issue, I'm now making use of a database to store the usernames and passwords instead

              • 4. Re: Flex and PHP Simple Login
                djh88ukwb Level 1

                Sorry to post again.  I thought i had this running well, making use of the Log in feature you had made.

                 

                I had it so it made use of a mysql database to store all the details, just like your example.  It then swapped to a different state when the correct details were given, or gave the error message when they wasnt.  It was all working well, until i exported the build.  Now it wont log in, once u hit the log in button, the text dissapears and nothing else happens.  Although if i run the app via flex it works fine. So it goes wrong when i export :S  The links to the scripts are the proper scripts ie http://www.dfsfd.upload.php  etc.

                 

                I even tried reverting back to the bare standard feature that you had on your website but i get the same problem :S

                 

                 

                Any ideas?

                • 5. Re: Flex and PHP Simple Login
                  Matt Le Fevre Level 4

                  check the location you've put in for the php scripts in your HTTPServices,

                   

                  make sure your release build location is setup properly in reference to them.

                   

                   

                  i imagine they are properly located when you're building, when when released your application no longer has a correct path to them.

                  • 6. Re: Flex and PHP Simple Login
                    djh88ukwb Level 1

                    Thanks for the quick reply.

                     

                    I hosted the scripts on a seperate server so in the flex app i put the full link to them. - http://www.blehbleh.com/upload.php.

                     

                    So i should imagine that should not be a problem ?

                    • 7. Re: Flex and PHP Simple Login
                      Matt Le Fevre Level 4

                      Post up all the code you're using, and i'll try testing it on my own server for you.

                       

                      If like you mentioned, it's working from the development environment but not after a release... the scripts being hosted on a different server shouldn't be an issue.

                       

                      There is probably something else amiss.

                      • 8. Re: Flex and PHP Simple Login
                        djh88ukwb Level 1

                        Hey,

                         

                        Thanks for that and sorry i have taken so long to reply.  I have since solved the problem.  I was hosting the scripts on a different server to the application and it didnt like it.  I have since moved them to the same server and it works perfectly.

                         

                        I have taken your example you linked and tried to make a couple of alterations.  Mainly added a third field to the Users Datagrid.  I have added a colum that displays whether users have logged in or not.  I added the field to the database  a simple Y, N enum field, where the default value is N.  I then added the extra column in the datagrid, and added the extra code into the getUsers file.  So my new datagrid shows the value, be it Y or N, taken directly from the daatabase.  However i am gettin  stuck on the Login file.  I need it so once a user logs in, it changes their "logged" value from N to Y.

                         

                         

                        I dont suppose you could offer a little insight into how i can achieve this could you please?

                        • 9. Re: Flex and PHP Simple Login
                          Matt Le Fevre Level 4

                          to alter a value already stored in the mysql database, you'll need to use the Update function, here's a quick tutorial

                           

                          http://www.w3schools.com/php/php_mysql_update.asp