There are a considerable number of options to complete this, and I’m sure that I’ll cover them some other time. However, in the mean time, I’ll show you the easiest way. The topics you should learn from this tutorial are probably as follows:
- PHP Basics
- PHP Simple concatenation
- PHP Variables and super-globals
- Passing data to flash
If you’re interested in passing variables straight through Flash, you can use loaderInfo.loaderURL; - for more information see Bruce’s comment. It’s worth noting that this works with AS3/CS3 only.
This involves PHP, so please check and be sure that your server is capable of parsing PHP. The main advantage of PHP as a server-side language is that the visitor doesn’t need to meet any particular requirements to view it. The basic concept is that the server opens the file, reads it, and does the commands in the <?PHP and ?> tags. The easiest is echo(); which just writes the variable you give it (brackets are not necessary), for example:
echo “This is some text”;
Next, you should learn that all variables start with $. So to define a variable and give it some text, I would write the following:
$variable = “This is some more text”;
it is important to note that there is nothing like strict data-typing like in Adobe’s actionscript. So, the next thing to learn is concatenation. To make two or more variables ‘connect’ to form one long string, you use the “.” syntax. It is possible to directly bring in data to a string by typing:
$variable = “Here is some more text.”;
echo “Here is some text. $variable”;
Note that there is “”, not ”. The apostrophe is more ’strict’ than the double quote, so it will not parse variables in strings. However, the performance speed of the server drops significantly, so natural concatenation is recommended. To do this, you need to type:
$variable = “Here is some more text.”;
echo “Here is some text. “.$variable;
Think of it like the + in javascript, or actionscript - the native concatenation syntax (syntax, if you didn’t know is the ‘language’ of the code, loosely like grammar but mainly the words).
The last thing to understand before the actual creation of the code is the super-global array. Many servers should have the original feature turned off now, as it is a huge security risk, but originally, URL parameters (e.g. ?variable=hello) like the example would have been accessed with $variable. However, to stop security risks, you should request parameters from accessing one of two arrays. They both start with $_ and are called GET and POST. You should recognise these as being the only way to send information by the http:// protocol (the one websites are sent through). POST should be used when there is a lot of information to process, like sending big forms, or if discretion of transfer is important, for example transmitting passwords. The one we’re interested in is GET. This is good if you want people to be able to link to the page that uses the GET parameter. Anything using GET will be displayed in the URL so people can simply copy and paste it to refer to the page. So to access the $_ get array, simply type this:
echo $_GET['variable'];
This will output anything from the URL: ?variable=pizza would make the script above output pizza onto the page.
Now, back to the objective. For example on a video player, we need to work out the URL of the FLV to play. A good example of this is on the video player at www.gotoandlearn.com. Visit a tutorial and see what I mean. To do this, lee Brimelow wrote a very simple PHP code to read the URL parameter and output the URL of the flash page to visit.
We’ll start by getting the URL of the Flash Movie, to which we’ll be passing the variable to.
<?PHP
// Empty
?>
Now we’ve got our basic PHP syntax ready. We’ve got the <?PHP ?> code ready which tells PHP to parse (do the commands in-) the code in between these tags. Put this at the top of the page (the location doesn’t really matter on an exact basis but it’s better to get variables ready in one place at the top, as well as it has to be before the actual flash movie).
The “//” Allows you to put whatever you like afterwards. Anything on that line will not be parsed. This is called commenting. Now we’re ready to write to code.
<?PHP
$swfUrl=”[put the address to the flash movie here]“;
?>
We’ve now defined a variable that you can put the URL of the flash movie in.
<?PHP
$swfUrl=”[put the address to the flash movie here]“;
$swfUrl.=”?movieUrl=”.$_GET['movie'];
?>
This is the most complicated part of the tutorial. It not only adds on a URL parameter to the Flash called movieUrl (we’ll come back to that), it is concatenating it with $_GET movie. That means that if you access the page with ?movieUrl=mymovie.flv it will pass it into Flash. You may think that $swfUrl.= sounds a bit weird, there’s a weird dot in front of the = sign. The reason for that is we’re concatenating (joining) it with itself, making the first part, and the bit following one long string.
Save your file as something like filename.php (not .html - otherwise it will not parse). If there is a problem, make sure that the extension is correct (on some servers it may be filename.php3).
We’ve got most of the PHP out of the way, just paste the code into your HTML page containing the Flash movie. Now, to make the URL work, when you import your Flash Movie into your page, don’t give it the URL to the Flash movie, instead, put this code in.
<?PHP echo $swfUrl; ?>
It will write the string into the page, and point the Flash movie to the correct URL. Now you’ve successfully sent the URL into Flash, from the HTML page. All you have to do now is make a dynamic text box in flash, with an instance of textBox. Add this actionscript to the timeline:
textBox.text=movieUrl;
Tada! Publish it and upload it. Open your PHP file and if you entered the Flash movie URL correctly, it should open and fill the textbox. Bear in mind how it will not work when you test the Flash movie in the IDE, and it will not work locally, unless your local computer is a server with PHP capability (I highly doubt it). If it doesn’t work, the likely problem is your server doesn’t support PHP, which is bad. There are (less fun, more complicated) alternatives that you can follow. Just do a forum search at www.gotoandlearnforum.com. Also use that link if you experience any problems, or just post a comment. If this helps, buy me a coke on PayPal or Digg this post.
Oh. Phew. That was a big one.
Great beginner tutorial for php Mr. Job…For you all out there who want to see an example of how powerful this language can be in conjunction with flash please view… Goto the voting section…Enjoy!!
Lawrence can you hyperlink http://www.deepakperwani.com …i closed the tag but it didnt work out properly…Cheers
It’s done, and remember, when posting comments on here, so long as it has http://www.mysite.com ’s sort of structure, it will link by itself.
I’m glad you liked the tutorial, and in light of one of your other responses, I’ve added a digg/delicious button to the end of all the posts.
Hey awesome blog! Know anyone who would be interested in part time admin’ing a Linux box? Hit me up!
Worked for me.
Thanks.
(Although much easier now with CS3 using loaderInfo.loaderURL;)
this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(myEvent:Event){
mid = loaderInfo.loaderURL;
mindex = mid.substr((mid.indexOf(”?mid=”)+5));//”mid” is var
midTxt.text= mindex;
callFunction();
}
Leave your response!
Categories
Recent Comments
Archives
Tags
'net .mac actionscript Adobe advert air Apple brilliant Browsing coming soon design flash flex Fun gadget Gadgets Google Internet internet explorer iphone iPod live Media Microsoft misc mobile ms News o2 os x pc Ramblings random rants Reviews skype social networking television tips twitter video welcome Windows wordpress YoutubeAuthors
Blogroll
Meta
Advertise
Who else?
Partners
Recent Posts
Countdowns
Most Commented
Most Viewed