Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


User authentication - Flash & PHP

1. Introduction

1.1 What we will do in this tutorial

The goal of this tutorial is to show how to build a simple user authentication graphic interface using Flash and Mysql as database. Here you can find the .sql file which will generate the table i use in this tutorial. Moreover here is the example that we will build.

Note: I used in the "forget" password section a form that will generate a new random password and displays it in the flash interface. It should be better if that new password will be mailed to the user and not returned like I did. Remember that all users' passwords are sotred in md5 format so you can't know them, there is no way to de-crypt them.

 

2. Start Up

2.1 Create the configuration file & the db table

(config.inc.php)
At first you have to create your MySQL table. You can use this query to build that table:
----------------------------------------------------
CREATE TABLE tutorial_user_auth (
userID int(20) unsigned NOT NULL auto_increment,
userName varchar(15) NOT NULL default '0',
userPassword varchar(32) NOT NULL default '0',
userMail varchar(255) NOT NULL default '',
userQuestion varchar(255) NOT NULL default '',
userAnswer varchar(255) NOT NULL default '',
PRIMARY KEY (userID),
UNIQUE KEY userMail(userMail),
UNIQUE KEY userName(userName)
) TYPE=MyISAM;
-------------------------------------

Once table is created, we have to set up our configuration file, which will describe our mysql variables:


<?php
    
// no cache
   
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
   
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
   
header("Cache-Control: no-store, no-cache, must-revalidate");
   
header("Cache-Control: post-check=0, pre-check=0"false);
   
header("Pragma: no-cache");
   
// displays only errors
   
error_reporting(E_ERROR);
   
// mysql connection variables
   
$host 'localhost';
   
$dbuser 'your_user';
   
$dbpass 'your_pass';
   
$dbname 'your_db_name';
   
$table 'tutorial_user_auth';
   
// connect to db
   
$db = @mysql_connect($host,$dbuser,$dbpass) or die("error=could not connect to $host");
   
$db mysql_select_db($dbname);
   if(!
$db)
   {
      print 
"error=could not connect to $dbname table";
      exit;
   }
?>

As yuo can see there are just the mysql variables and a variable $db which is our Mysql connection we will use in all others files.

2.2 Functions' files

(functions.php)
In order to make some check on the user inout values set up a new document (function.php) with this code:

            
<?
function valid_email($email){
   
// check if email is valid
   
if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"
   
."@([a-z0-9]+([\.-][a-z0-9]+))*$",$email$regs)){
      return 
false;
   } else if( 
gethostbyname($regs[2]) == $regs[2] ){
      
// if host is invalid
      
return false;
   } else {
      return 
true;
   }
}

function 
valid_userName($name){
   
// check valid input name
   
if(!eregi("^[a-z0-9]{8,15}$",$name)){
      return 
false;
   } else {
      return 
true;
   }
}

function 
valid_password($pwd){
   
// check valid password
   
if(!eregi("^[a-z0-9]{6,8}$",$pwd))
   {
      return 
false;
   } else {
      return 
true;
   }
}
?>

In this files we have created 3 different functions:
function valid_email: it makes a first check on the email address of the user with the an eregi expression (which is a case insensitive regular expression) and then another check on the IP address of the email domain with the gethostbyname function.

function valid_userName: Again with the eregi expression checks if username contains only alphanumerical value and its length is between 8 and 15 chars

function valid_password: similar to the valid_userName function, but the passed string must be between 6 and 8 chars

Ok, now we will go to analyze the user.php file which will include these two php file we created.