Flex :: AMFPHP Login
Submitted by hasan on Mon, 07/10/2006 - 07:00.
The subject pops up a lot about reusable Flex components. Here's one to handle multi-user logins via AMFPHP / MySQL:
< ?xml version="1.0" encoding="utf-8"?>
<mx :Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" width="355" height="213">
</mx><mx :Script>
< ![CDATA[
import flash.net.Responder;
public var gateway:RemotingConnection;
public function checkLogin(service:String,params:Array):void
{
gateway = new RemotingConnection("http://yourwebserver.com/flashservices/gateway.php");
gateway.call(service,new Responder(onResult,onFault),params);
}
public function onResult(result:Array):void
{
if (result[0]!="VALID")
{
lResponse.text = result[1];
}
else
{
lResponse.text = result[1];
parentDocument.currentState='Main';
}
}
public function onFault(fault:String):void
{
lResponse.text = "Service Error: "+fault;
}
private function checkInput():void
{
if(tUser.text == "" || tPass.text == "")
{
lResponse.text = "Please complete all fields";
}
else
{
lResponse.text = "Logging in...";
checkLogin("login.Verify.validate",[tUser.text,tPass.text]);
}
}
]]>
</mx>
<mx : Panel height="200" layout="absolute" title="Admin Login" left="0" right="5" bottom="13">
<mx :Label x="33" y="45" text="Username:"/>
<mx :Label x="33" y="85" text="Password:"/>
<mx :Button x="178" y="118" label="Login" id="bLogin" tabIndex="3" click="checkInput()"/>
<mx :TextInput x="113" y="83" id="tPass" tabIndex="2" displayAsPassword="true"/>
<mx :TextInput x="113" y="43" id="tUser" tabIndex="1"/>
<mx :Label x="113" y="10" id="lResponse"/>
</mx>
And here's the AMFPHP service that the component connects to:
< ?php
class Verify {
function Verify(){
$this->methodTable = array(
"validate" => array(
"access" => "remote",
"description" => "Return the login results"
)
);
}
function validate($nfo){
$dbc = mysql_connect("localhost","username","password");
mysql_select_db("yourdatabase");
$user = $nfo[0];
$pass = $nfo[1];
$query = "SELECT DISTINCT * FROM admin WHERE aname = '$user' AND acode = '$pass'";
$rs = mysql_query($query);
$var = mysql_fetch_assoc($rs);
$u = $var["aname"];
$p = $var["acode"];
if ($pass != $p){
$response[0] = "INVALID";
$response[1] = "Invalid username and/or password";
} else {
$response[0] = "VALID";
$response[1] = "Welcome $u";
}
return $response;
}
}
?>

