Password protected content in Flash with ActionScript 3 and PHP

This morning I was browsing through the gotoAndLearn forum and I saw a question about how to make a password protected section in a Flash site. So, I made a quick example and I thought I should share it with all my readers too ;)

You can download the source files from here:

  Password Protected Flash Content (800.5 KiB, 537 hits)

Here you can see the ActionScript 3 code:

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.showDefaultContextMenu = false;
 
//path to the php file on your server
var phpPath:String = "login.php";
 
//make password text field (when you type the characters will be like "******")
pass.displayAsPassword = true;
 
login.addEventListener(MouseEvent.MOUSE_DOWN, loginDown);
 
function loginDown(e:MouseEvent):void{
	//check to see if something in both the user and pass text fields
	if (user.text != "" && pass.text != "") {
		sendLoadData();
	}else{
		trace("Please fill in both username and password");
	}
}
stop();
 
function sendLoadData():void
{
	var dataRequest:URLRequest = new URLRequest(phpPath);
	dataRequest.method = URLRequestMethod.POST;				
 
	// define the custom parameters that will be sent to the .php file
	var params:URLVariables = new URLVariables();
	params.user = user.text;
	params.pass = pass.text;
 
	dataRequest.data = params;
 
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
	urlLoader.addEventListener(Event.COMPLETE, urlLoaderComplete);
 
	try
	{
		urlLoader.load(dataRequest);
	}
	catch (event:Error)
	{
		trace("Incorrect PHP file path.");
	}
}
 
function urlLoaderComplete(event:Event):void
{
	// once all the data has been sent and we'll check for a message sent
	// from the php file to tell us if the operation has ended with success or not
	errorHandler(event.target.data.secure_response);
}
 
function errorHandler(message:Number):void
{
	// check the message that was sent back from the php file
	//trace(message)
	if (message == 1)
	{
		gotoAndStop(2);
	}
	else
	{
		trace("Wrong Username or Password");
	}
}

And here you have the PHP code:

<?
//Your set username and password
$username = "vamapaull";
$password = "mypassword";
 
 
//Variables received from ActionScript
$user=$_POST['user'];
$pass=$_POST['pass'];
 
 
//Chack them against each other
if ($user == $username && $pass == $password){
	print "secure_response=1";
}else{
	print "secure_response=2";
}
 
?>

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Comments »

 
  • james says:

    Hi vamapaull,
    How do you add a list of usernames and passwords into the php script? Do you declare them as an Array if that is possible in php?
    I am trying to set up a flash “gate”. But i unable to change your php code to add a list of usernames and passwords. I also want to send users to different frames in my flash gate. For example password1 will load frame1, password2 will load frame2 After days of testing and searching the internet I have made no progress. Can you help me or give me a hint ?
    That would be awesome!!
    james

  • Unregistered says:

    Nice. Your blog is bookmarked.

    Haven’t tested this yet though.

  • Lazaro says:

    Very good. Thank you.

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">