Part 3 - The PHP Backend
The PHP script is a simple script that queries the database with the details provided to see if the user is real or not, and the outputs the result in XML.
[php]<?php
define( "DATABASE_SERVER", "localhost" );
define( "DATABASE_USERNAME", "user" );
define( "DATABASE_PASSWORD", "pass" );
define( "DATABASE_NAME", "flex" );
//connect to the database
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD) or die(mysql_error());
//select the database
mysql_select_db( DATABASE_NAME );
//asign the data passed from Flex to variables
$username = mysql_real_escape_string($_POST["username"]);
$password = mysql_real_escape_string($_POST["password"]);
//Query the database to see if the given username/password combination is valid.
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_fetch_array(mysql_query($query));
//start outputting the XML
$output = "<loginsuccess>";
//if the query returned true, the output <loginsuccess>yes</loginsuccess> else output <loginsuccess>no</loginsuccess>
if(!$result)
{
$output .= "no";
}else{
$output .= "yes";
}
$output .= "</loginsuccess>";
//output all the XML
print ($output);
?>[/php]
Explanation:
Again, if you have some background in PHP this should be extremely basic stuff. I"ve commented most of the code so you should be able to understand it.
After connecting to the MySQL database, we query the db to see if the given details(username and password) are valid ones. If so, we output it as XML (<loginsuccess>yes or no depending on whether or not the operation was successful) and Flex then reads it.
Earlier in this tutorial we wrote the following:
if(login_user.lastResult.loginsuccess == "yes")
{
currentState = "Logged In";
}
lastResult.loginsuccess refers to the <loginsuccess> we outputted from PHP. So if instead of outputting <loginsuccess>, you decided to output <userLogin>, then you would change lastResult.loginsuccess to lastResult.userLogin. Make sense now?
Now that we cleared that bit up, on to the last part, creating the Logged In View State and throwing in some neat effects!
Previous 1 2 3 4 5 Next Print