Saturday, February 19, 2005

Authentication and Request Variables

My first actionscript test was done today. I tried to see how authentication would work as well a getting filenames or variables from the url.

Authentication


The authentication thing is quite easy, create two input boxes (one single line [username_txt] the other password [password_txt]) and make a button for submit in the first frame. Then add a onrelease action for the button as follows:

username = this.username_txt.text;
if (username == "test" & this.password_txt.text == "test")
{
gotoAndStop(2);
}

Next in frame say something like "Welcome test" using a text box [welcome_txt]. I did the following:

this.welcome_txt.text = "Welcome " + username + "\n" + "You have requested " + _root.request;

Fake File Names and Request Variables


What's nice about actionscript is it reads in the request variables automatically. If the url is "http://domain/test.swf?myname=joe&age=45" then the actionscript will automatically have the variable myname and age stored.
The tricky part comes when you want to take the url from html/php file and add it to the swf's address. So what you have to do in your php is add the request query to end using the $_SERVER["QUERY_STRING"] variable. Therefore src="basic-AuthReqVars.swf" ... becomes src="basic-AuthReqVars.swf?" ... .
The other tricky part would be to include a fake filename. For instance, with php I might want to say "http://domain/index.php/289590.jpg" but it really it goes to index.php and the filename is parsed out.
So what i do is in the PHP is create a variable for the new query string and use that to get the filename or id.

if (isset($_REQUEST["id"]))
{
$req = $_SERVER["QUERY_STRING"];
$dot = "";
}
else
{
$dot = "../";
$req = "file=" . substr($_SERVER["PATH_INFO"],1);
}

The $dot variable is important because of the way the html will read where the swf is. If I goto "http://domain/index.php/289590.jpg" it will think that the swf which is referenced at "test.swf" will think it is in the directory index.php but in fact it is in the root.
Then in my src I call it with "src="basic-AuthReqVars.swf?" ..." .
Then in the first frame of the animation, I say:

if (_root.id)
{
request = "id#" + _root.id;
}
else
{
request = "file " + _root.file;
}

In the second frame, I say:

this.welcome_txt.text = "Welcome " + username + "\n" + "You have requested " + _root.request;

If you have any question feel free to post comments.
Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?