0

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
  • Use ExternalInterface to call a JavaScript method from Flash [and vice versa]. I'm unclear if that will solve your problem. Commented Mar 7, 2011 at 21:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think External Interface was exactly what i needed! I have it working! Thanks! didn't know about it till now.
0

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).

2 Comments

You bringing up the word sessios, made me think... Maybe I should just be creating a sessionid when the page is loaded, maybe using something like md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) then i can load that var into flash when the username is added to the DB...then I have the sessionID on the HTML side since it was created there....and just use that to reference which row to delete with mysql on my jquery .unload function
You can definitely do that. What I recommend, though, is that you change the system so that the server generates the username and not the flash. You could place a call to a php script (using the above). The script could put together the session info that you mentioned, insert a record in the db for that username and session, then return the uid to your flash app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.