问题
I was hoping maybe someone could provide some insight on a problem I'm having a tough time deciding how to solve.
I have a rather simple flash application users can make a quick username when connected, and the username is created inside the flash swf.
Now, I have a cron job deleting inactive usernames every ten minutes (on my mysql database where these usernames are all stored and accessed by the other people online) which is fine. But it can still get cluttered up if a bunch of people sign off at once, there is still that 10 minute window before the cron job clears them.
The users have an option to click log out in the flash application which is fine and works great. But of course many choose not to click log off they just click the browser x.
I've looked into onbeforeunload and jquery's .unload but I still need a way to get the username variable that's IN flash INTO the HTML, then use a php script to run the delete username mysql query.
Is there an easier solution?
If not, any insight on how I might pass the username variable to the html to hold onto it after the user makes their username so it can be involved with the .unload function running the php script?
EDIT::::: Maybe is there a way to create a UNIQUE string of numbers with php then pass that var to flash to include with the mysql row then since i already have that var since it was created on the html side, just along with the unload, have it delete the row that has that unique id?
If anyone things this idea would be the best approach, and if i used something like md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) to make a random iD how could i go about storing the result in a var i could place in the flash vars param then again in the jquery unload or javascript onbeforeunload if that would be better . im just more familiar with jquery
回答1:
You could actually invoke an ExternalInterface command to populate a hiddenfield in your HTML coming from your SWF component.
if (ExternalInterface.available)
{
var js:String = "yourJavaScriptFunction";
var valToPass:String;
ExternalInterface.call(js(valToPass));
}
And in your HTML page, you write a javascript function:
<script language="javascript" type="text/javascript">
function yourJavaScriptFunction(valToPass)
{
document.getElementById('yourHiddenField').value = valToPass;
}
And from the unload event fired up by your page, you can access the value which was passed from your SWF.
Take note that you can call the javascript function from your SWF as soon as you get the login credentials of your user.
Hope this helps.
回答2:
You want to use a combination of URLRequest, URLVariables and URLLoader to do this. See the below.
var myData:URLRequest = new URLRequest("some.php");
myData.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.username = 'CREATED USERNAME';
myData.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myData);
function dataOnLoad(evt:Event){
trace('Completed');
}
This would then call the 'some.php' file with your username stored in '$_POST['username']'. Your php script can then make an impression on the db linking that username to that session (however you want to do that).
来源:https://stackoverflow.com/questions/5225202/passing-variable-from-flash-to-html-php