3 Replies Latest reply on Dec 8, 2009 3:32 AM by Matt Le Fevre

    How to create a login session in flex?

    megharajdeepak Level 1

      Hi there,

       

      I have a flex application with two states. One is a "login" state which is a screen with username and password. The other state is "loggedIn" state which is a screen with a small application. As soon as the user credentials are checked from the back end, based on it's success, I'm changing the state to "loggedIn", else there is a message to the user about login failure in "login" state itself.

       

      My problem is if I'm in "loggedIn" state and if I hit refresh button of the webpage(F5), it goes back to the login screen i.e., it just restarts the application..

       

      How can I control this? Is there something like login session which can do the trick?

       

      Appreciate your help...

       

      -Deepak

        • 1. Re: How to create a login session in flex?
          Matt Le Fevre Level 4

          You can do this via PHP with cookies/sessions.

           

           

          via Cookies;

           

          on successful login, initiate a PHP script that'll save login information to a cookie. When the app loads have a function to check to see if a cookie is set, if it is then load it + information & perform an auto login.

           

          via Sessions;

           

          on successful login, initiate a PHP script that'll save login information to the Session. Whenever the app starts, perform a session check and if one exists, retrieve the information from it.

           

           

           

          I also believe Flash has it's own version of this kind of stuff built in, but i can't for the life of me remember what it's called.

          • 2. Re: How to create a login session in flex?
            megharajdeepak Level 1

            PHP!!! But the backend I'm using is SAP(webservices from SAP)

            I don't know PHP programming..... Sample code please?

            • 3. Re: How to create a login session in flex?
              Matt Le Fevre Level 4

              Flex code:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="getSession.send()">
                  
                  <mx:Script>
                      <![CDATA[
                          import mx.rpc.events.ResultEvent;
                          
                          
                          public function getSessionStuff(evt:ResultEvent):void
                          {
                              saveInfo.text = evt.result.info;
                          }
                          
                      ]]>
                  </mx:Script>
                  
                  <mx:HTTPService id="startSession" showBusyCursor="true" method="POST" url="http://localhost/startSession.php">
                      <mx:request xmlns="">
                          <stuffToSave>
                              {saveInfo.text}    
                          </stuffToSave>
                      </mx:request>
                  </mx:HTTPService>
                  
                  <mx:HTTPService id="getSession" result="getSessionStuff(event)" showBusyCursor="true" method="POST" url="http://localhost/getSession.php"/>
                  
                  <mx:Button x="602" y="323" label="Start Session" enabled="{saveInfo.text != ''}" click="startSession.send()"/>
                  <mx:TextInput id="saveInfo" x="563" y="293"/>
                  
              </mx:Application>
              
              

               

              setSession.php code;

               

              <?php
              
                  session_start();
              
                  $_SESSION['loggedIn'] = $_POST["stuffToSave"];
              
              ?>
              
              

               

              getSession.php code;

               

              <?php
              
                  session_start();
                  
                  if($_SESSION['loggedIn'])
                  {
                      echo "<info>".$_SESSION['loggedIn']."</info>";
                  }
                  else
                  {
                      echo "<info>nothing has been saved</info>";
                  }
              
              ?>
              

               

               

              ta daa