PDA

View Full Version : Flex 2/ AMFPHP login woes


camcim
11-23-2006, 08:58 AM
Hi,
Currently trying my hand at Flex 2. Not doing to bad so far.
I have however run into an issue with a login script. I'm wanting to use a combination of Flex and AMFPHP to create a login/user auth environment.
I've been successful receiving data from a mysql backend, just not sending and verifying data.
This is what i have so far:
pmpak.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" horizontalAlign="left" verticalAlign="top">
<mx:Script>
<![CDATA[

import flash.net.Responder;

public var gateway:RemotingConnection;
public var response:Responder;
public var gatewayURL:String = "http://localhost/flashservices/gateway.php";

public function addData(userLogin:String,params:Array):void{

//Check if the username have at least 4 chars
if (username.text.length<4){
error_display.text = "Please enter username (minimun 4 characters)";
return;
}
//Check if the username have at least 4 chars
if (password.text.length<4){
error_display.text = "Please enter password, minimun 8 characters";
return;
}

//Show a waiting message
error_display.text = "Checking user/pass. Please wait...";
return;
//Disable the push button
//submit_user.setEnabled = false

response = new Responder(onResult,onFault);
gateway = new RemotingConnection(gatewayURL);
gateway.call(userLogin,response,params);
}

public function onResult(result:String) : void
{
//status_display.text = result;

var user_details:String = result[0];
var user_permissions:String = result[1];
var primary_details:String = result[2];

if (user_details['pm_user_id'] != "" && user_permissions['admin_access'] == "1"){
status_display.text = "Admin account";
//trace(result);
}
}

public function onFault(fault:String) : void
{
status_display.text = fault;
//trace(fault);
}
]]>
</mx:Script>
<mx:Panel width="352" height="285" layout="absolute" verticalCenter="-40.5" title="Login" horizontalCenter="-180.5" horizontalAlign="center" verticalAlign="middle">
<mx:TextInput x="86" y="57" id="username"/>
<mx:TextInput x="86" y="87" id="password" displayAsPassword="true" enabled="true"/>
<mx:Label x="17" y="59" text="Username" textAlign="left" fontWeight="bold"/>
<mx:Label x="18" y="89" text="Password" textAlign="left" fontWeight="bold"/>
<mx:Button x="86" y="117" label="Login" id="submit_user" click="addData('userLogin.authenticate',[username.text,password.text])"/>
<mx:TextArea x="86" y="17" id="error_display" width="225" color="#ff0000" fontWeight="bold" borderStyle="none" textAlign="left" editable="false" height="34" wordWrap="true" />
<mx:TextArea x="86" y="147" id="status_display" width="225" color="#000000" fontWeight="bold" height="88"/>
</mx:Panel>
</mx:Application>

and userLogin.php (in the flashservices/services dir)

<?php
session_start();

class userLogin {

function userLogin(){
$this->methodTable = array(
"authenticate" => array(
"description" => "Check for a username and password",
"access" => "remote", // available values are private, public, remote
"arguments" => array ("username", "password")
)
);
// Initialize db connection
include("../../include/appconstants.php");
}

function authenticate($username, $password){

$strQuery = "select * ";
$strQuery = $strQuery."from pm_users, pm_company ";
$strQuery = $strQuery."where pm_users.pm_company_id=pm_company.pm_company_id ";
$strQuery = $strQuery."and pm_users.pm_username='$username' "; // u_name
$strQuery = $strQuery."and pm_users.pm_password='".md5($password)."'"; // p_word

$objRS = ExecuteQuery($strQuery, DBSERVER, DBUID, DBPWD, DBDATABASE);

$intNumResults = mysql_num_rows ($objRS);

if ($intNumResults > 0)
{

while ($user = mysql_fetch_object($objRS))
{
$_SESSION['user_details']['pm_user_id'] = $user->pm_user_id;
$_SESSION['user_details']['pm_user_type'] = stripslashes($user->pm_user_type);
$_SESSION['user_details']['pm_first_name'] = stripslashes($user->pm_first_name);
$_SESSION['user_details']['pm_last_name'] = stripslashes($user->pm_last_name);
$_SESSION['user_details']['pm_company_id'] = $user->pm_company_id;
$_SESSION['user_details']['pm_company_name'] = stripslashes($user->pm_company_name);
}

$strQuery = "select pm_company_id, ";
$strQuery = $strQuery."pm_company_name, ";
$strQuery = $strQuery."pm_company_type ";
$strQuery = $strQuery."from pm_company ";
$strQuery = $strQuery."where pm_company_id = '1' ";
$strQuery = $strQuery."and pm_company_type = 'default' ";

$objRS = ExecuteQuery($strQuery, DBSERVER, DBUID, DBPWD, DBDATABASE);

while ($row = mysql_fetch_object($objRS))
{
$_SESSION['primary_details']['pm_company_id'] = $row->pm_company_id;
$_SESSION['primary_details']['pm_company_name'] = $row->pm_company_name;
$_SESSION['primary_details']['pm_company_type'] = $row->pm_company_type;
}

// Get admin permissions into session array
$strQuery = "select * ";
$strQuery = $strQuery."from pm_admin_user_permissions ";
$strQuery = $strQuery."where pm_user_id = '".$_SESSION['user_details']['pm_user_id']."'";

$objRS = ExecuteQuery($strQuery, DBSERVER, DBUID, DBPWD, DBDATABASE);

while ($row = mysql_fetch_object($objRS))
{
$_SESSION['admin_user_permissions']['admin_access'] = $row->admin_access;
$_SESSION['admin_user_permissions']['add_company'] = $row->add_company;
$_SESSION['admin_user_permissions']['edit_company'] = $row->edit_company;
$_SESSION['admin_user_permissions']['delete_company'] = $row->delete_company;
$_SESSION['admin_user_permissions']['add_users'] = $row->add_users;
$_SESSION['admin_user_permissions']['edit_users'] = $row->edit_users;
$_SESSION['admin_user_permissions']['delete_users'] = $row->delete_users;
$_SESSION['admin_user_permissions']['add_project'] = $row->add_project;
$_SESSION['admin_user_permissions']['edit_project'] = $row->edit_project;
$_SESSION['admin_user_permissions']['delete_project'] = $row->delete_project;
}

$authenticate = array($_SESSION['user_details'], $_SESSION['admin_user_permissions'], $_SESSION['primary_details']);
return $authenticate;

}
else
{
return false;
}

}
}
?>

Any ideas as to where i might be going wrong? I'm not getting any errors either.

Cheers