User Login with Actionscript 3 and AMFPHP
Recently I’ve been experimenting with some components and frameworks for Flash CS4 in order to shave off some development time of a rather large application I’m building. And then I found, this later in the game, AMFPHP. Altough it’s not a framework by itself (it’s more like a Web Service), and the fact that it is now much more Flex-oriented than Flash, it can still save you plenty of time when dealing with serverside components from Flash.
One of my first attempts was to create a rather simple user login, comparing the user data from an actual MySQL database. The greatest thing about AMFPHP is that you don’t have to write anyspecific code to deal with the PHP data output format. This means no XML output, and therefore cleaner code from both ends. Continue to tutorial and sample files.
So here’s my Actionscript 3 package:
package { import flash.display.MovieClip; import fl.events.*; import flash.events.*; import flash.net.NetConnection; import flash.net.Responder; import fl.data.DataProvider; public class Main extends MovieClip { private var gateway:String = "http://localhost/amfphp/gateway.php"; private var connection:NetConnection; private var responder:Responder; private var loginResult:Array = new Array(); private var whoIsLogged:int; public function Main() { submit_btn.addEventListener(MouseEvent.CLICK, sendData); responder = new Responder(onResult, onFault); connection = new NetConnection; connection.connect(gateway); pbar.visible=false; } public function sendData(e:MouseEvent):void { connection.call("UserLogin.loginer", responder, user_txt.text, pass_txt.text); pbar.visible=true; } private function onResult(result:Object):void { pbar.visible=false; loginResult = result.serverInfo.initialData; //[0][0] stores the user ID. //[0][1] stores the user's name. if (loginResult.length==1) { resposta_txt.text = "Hello " + String(loginResult[0][1])+"."; whoIsLogged = loginResult[0][0]; } else if (loginResult.length==0) { resposta_txt.text = "Wrong username/password."; } } private function onFault(fault:Object):void { trace(String(fault.description)); resposta_txt.text = "Server failure." } } }
The AS code is pretty straight forward: when the user clicks the Submit button, it dispatches the “sendData” function, which by itself makes the connection to the AMFPHP Gateway and sends two parameters: the username and password text fields.
As our PHP code will check for a username and password match on the database, it will either output one result or none at all (assuming you’ve taken care of database redundancy, if it’s necessary). This means that if the combination is correct, the length of the loginResult array, which contains the server’s response, will be 1. If the data doesn’t match, no output will be sent and the array will be empty, dispatching the error message.
This is the PHP code. The name of this file should represent the Class we specified on the Actionscript code. So you should save this file in the $yourserver/amfphp/services path, with the name of “UserLogin.php“. If you want to change this name, don’t forget to change both the name of the Class inside, and its AS reference.
class UserLogin { var $dbh; public function __construct() { $this->dbh = mysql_connect ("localhost", "admin", "your_password") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("your_database"); } function loginer($id,$pass) { return mysql_query(sprintf("SELECT * FROM users_tb WHERE name='$id' AND password='$pass'")) ; } }
Don’t forget to change your parameters according to your DB. Here I’m comparing the “name” and “password” fields of the “users_tb” table. Change these, as well as your MySQL username and password and database name, as necessary.
Notice that the only thing that PHP is returning is the pure RAW output of the mysql_query function. AMFPHP is capable of catching this data just by giving it the name of the ‘Class.Method‘ of this simple service we’ve created. And please be aware that this example is extremely simple and it is not foolproof when system security is concerned, I simply wanted to make as simple as possible.
Useful post!
I just downloaded the source, but the .fla seems corrupt.
may you fix it?
thanks
N