[AS2][PHP] Getting all GET and POST vars into FlashVars

Hello, I wasn’t sure whether to post this here or in the server-side forum, but I did it here since this is more of a helpful hint than anything :mountie:

I am sure this might be common sense for all you long time developers out there.
But I figured I would post this for the flash people just now getting into the full server-side development side of flash. This is just a kind of practice more than anything. It’s a way to get all variables on the html page, into the FlashVars parameter of an swf object, regardless of var name and whether it is a GET or POST variable.

First we do a little php to get all the vars into a string variable called $flash_vars. This of course can be done differently, with a function, or class, etc… but again, this is just for example. What this is doing, is actually saving all the GET and POST variables into a javascript string to print later within the swfobject. Notice the prefix of “fv_” for each of the variable names. This is important so Actionscript can differentiate between _root properties, and the FlashVars saved in this way.

<?php
//
if(count($_GET) > 0 || count($_POST) > 0) {
	$flash_vars = "";
	foreach ($_GET as $var => $value) {
		$flash_vars .= 'so.addVariable("fv_'.$var.'", "'.$value.'");';
	}
	foreach ($_POST as $var => $value) {
		$flash_vars .= 'so.addVariable("fv_'.$var.'", "'.$value.'");';
	}
} else {
	$flash_vars = 'so.addVariable("fv_error", "No flashVars defined");';
}
?>

You of course don’t have to use deconcept’s swfobject for this, it just cuts down on A LOT of code, and it is what I use, so you can just use this for the idea is all :nerd:
USING SWFOBJECT:


<div id="flashcontent"></div>
<script type="text/javascript">
	/*
	* SWFObject v1.5: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
	* SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License:
	* http://www.opensource.org/licenses/mit-license.php
	*/
	var so = new SWFObject("main.swf", "main", "700", "500", "8", "#FFFFFF");
	<?php print($flash_vars); ?>
	so.write('flashcontent');
</script>

This code is assuming you have a textbox called myTextBox already set up to do a live run-time trace with. The function returns an object with property names with the same names of the GET and POST variables. It finds them by the prefix we set earlier, but this is removed in the process. When finished you have an object containing all the variables with names and values that were set by GET and POST data within the html page.

AS2:


function getFlashVars():Object {
	var obj:Object = {};
	for(var fv in _root) {
		var prefix:String = fv.substr(0, 3);
		var varName:String = fv.substr(3, fv.length);
		if(prefix == "fv_") {
			obj[varName] = _root[fv];
		}
	}
	return obj;
}
var flashVars:Object = getFlashVars();
var traceText:String = "";
for(var fv in flashVars) {
	traceText += fv+" = "+flashVars[fv]+newline;
}
myTextBox.text = traceText;

AS3 METHOD EQUIV:


public function getFlashVars():Object {
	var obj:Object={};
	for (var fv in root.loaderInfo.parameters) {
		var prefix:String=fv.substr(0,3);
		var varName:String=fv.substr(3,fv.length);
		if (prefix == "fv_") {
			obj[varName]=root.loaderInfo.parameters[fv];
		}
	}
	return obj;
}