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
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.
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).
Related
The title may be a bit non-descriptive, so let me explain what I want to do.
I have a lot of JavaScript functions that I want to be able to "give access to" based on a user's permissions to do certain things on my system. As an example:
I have a JS function to add an employee:
function doNewEmployee() {
var x;
var objTemp=new Object();
var myElements = new Array ("username","password1", "password2", "emp_type",
"name", "surname", "personal_contact", "work_contact", "location_dd",
"home_address", "access_level", "id_num");
for (x=0;x<myElements.length;x++){
objTemp[''+myElements[x]+'']=document.getElementById(myElements[x]).value;
}
asyncProxy.addNewEmployee(objTemp);
}
This sends all the parameters through AJAX to my PHP
Now, what I want to be able to do is check whether the user has permissions to execute this function and only include it in the final JS file sent client-side if this is true.
Is this possible? I was thinking about just creating a load of if statements, echoing out the javascript functions in the index.php file, but this would entail having all my JS inline, which isn't what I want.
I guess at the end of the day I want a dynamically built JS file?
Any help would be greatly appreciated!
I'm creating Flash "memory" game, Idea to discover 2 equal cards. Here is timer which count time in how many seconds all cards are discovered. After complete game timer stops. How can I get this time from swf file and write to database? As I understand I need take this data with php file, but don't have idea how to do It.
As I know I need to do something like that:
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, scoreSaveResponse,false,0,true);
var request:URLRequest = new URLRequest("http://mysite.com/score.php");
var urlVars:URLVariables = new URLVariables();
urlVars.time = timer.currentCount;
urlVars.userName = "yourUserName";
request.method = URLRequestMethod.POST;
urlLoader.data = urlVars;
urlLoader.load(request);
function scoreSaveResponse(e:Event):void {
//whatever you return from the php page is found in urlLoader.data
}
//LondonDrugs_MediaServices's code
But could someone explain me how to do It, because If I use this part of code nothing happens, I don't get any errors, but I don't know how to use this in php or how to write it to database.
My app will be used for Facebook, that's mean username will be used the same as in Facebook. So I think first i need to get time to PHP than get username from Facebook and write both to database.
Thank you for answers.
Try to use AMFPHP.
AMFPHP makes it possible to write a service for your application where you can store your SWF values into a PHP database.
http://www.silexlabs.org/amfphp/
I am trying to make a chat room on my website, I am using php and mysql to store the messages and all the info. How could I automatically refresh the page every time someone updates the database? example:
If I am on my site, the messages show up on my screen but I can only see more recent messages after I refresh the page. Is there a way to make it real-time?
Also I do not know much javascript/ajax/jquery or any of that. Any help is appreciated!
There will be low amount of traffic on my site. Probably around 10-15 people at a time, if that even.
Your best bet is to make an AJAX request every sec or so and see if there are new messages.
You probably do not want to be reloading the page every time. My recommendation, and there are many ways to do this, is to make a ajax call every so often and check/pull the new information from the database.
I would research AJAX and do a tutorial.
This would be accomplished through ajax by calling a function and updating the div. I would not suggest making people refresh a page everytime they send a message it would get ugly. Another option would be using HTML5 web workers
http://msdn.microsoft.com/en-us/hh549259.aspx
You are going to need to learn AJAX in order to make this work well, and jQuery is probably the easiest way to do it. If we can assume that the DIV you want to update has the ID PonyRides, you would want to do:
$("#PonyRides").ajax({url: "/chat.php?getupdates=true"});
This will get the contents of chat.php and stick it into the #PonyRides DIV. This assumes that chat.php will get the contents of the database and format them into HTML.
The remaining challenge is to make it update whenever your database does, but the simplest way is just to reload the whole chat regardless of whether an update has been made or not.
That will impact performance, but if you have less than a hundred chatters you'll probably be fine. If you have more than that, you'd do well to sense inactivity and decrease the checking period, or only send updates instead of the whole chat. Those are more complicated topics, though, and you can build them in as needed once you get these basic concepts down.
To do this, simply wrap the ajax() call in an interval like so:
setInterval(function(){ //the following code runs repeatedly
$("#PonyRides").ajax({url: "/chat.php?getupdates=true"}); //update our chat div
},5000); //repeat every five seconds
The other, awful method would be to load chat in an iFrame, set to reload periodically using the meta refresh technique. This would be dreadful, and can only be recommended if you are trying for some reason to support incredibly old browsers.
You can use AJAX request to update the values
<script type='text/javascript'>
// function for making an object for making AJAX request
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}
var http899 = getXMLHTTPRequest();
function searchFabIndia() {
var myurl = "http://my2nddomain.com/yebhi.php";
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http899.open("GET", modurl, true);
http899.onreadystatechange = useHttpResponse899;
http899.send(null);
}
function useHttpResponse899() {
if (http899.readyState == 4) {
if(http899.status == 200) {
// do all processings with the obtained values / response here
// after doing the stuff, call fn again after 30 s say
setTimeout("searchFabIndia()", 30000);
}
}
}
</script>
<body onload='searchFabIndia();'>
I would suggest making an AJAX request to a file on your server which will update the database. If the update to the database is successful then return the message which was updated. Back on the client side you wait for the response and if you get one then append the message to the end of the content. This way you're loading all the messages every time (which would be expensive), you're only loading new messages.
There must be something similar to SignalR(.net) for php. It lets you add code when an event occurs, I think that is what you are looking for.
I'm using facebook php sdk to get friend list of an user. But I'm strugling to get that list to flash.
The idea is that the list is always new, depending from the user loading the application. So what I need is one .html file which is loaded with php script to proccess friend list and embedded flash .swf to which php would pass information.
How can I achieve this? thanks
There are two ways to do it:
Set the flashvars to the data you want to pass to the Flash file. Since the friend list rarely changes, that should work.
Or use a URLLoader:
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://example.com/yourscript.php");
loader.addEventListener("complete", loader_complete);
loader.loader(request);
private function loader_complete(event:Event):void {
var scriptData:String = event.currentTarget.data;
// process data
}
I have a main page (topic.php) with GET information in the URL, like this:
http://studio.byuipt.net/topic.php?topic=Debugger&desc=Helps%20find%20and%20solve%20problems%20with%20others%27%20code.
I have a div, "currLeader" in topic.php into which I load another page, getCurrLeader.php. getCurrLeader.php is supposed to use the topic variable in the $_GET info of the url to do a mysql search and return the relevant info. The problem is that while, scripts on topic.php are able to successfully use extract($_GET), I am not able to retrieve any variables out of the getCurrLeader.php extract($_GET) statement. I thought both pages would be able to access the currently showing url. Is there another way I can get this information out of the current url?
(consequently, the "topic" info is actually present in an element with an id on the page, and I'm able to successfully retrieve it using jquery, but I can't figure out a way to then, within the same file, pass that value to my php script).
I'm not really sure I understand what you're asking. On first read I assumed you were trying to do this with jquery, but now I'm not so sure I'm on the same page at all. Here's an easy way to extract the parameters in javascript:
<script type="text/javascript">
var ourlocation = location.href;
var thisstuff = ourlocation.split("?");
var id = thisstuff[1];
var idary = id.split("&");
var param2 = idary[0];
var param3 = idary[1];
var param4 = idary[2];
</script>
Which probably has nothing to do with what you're trying to do.
On 2nd read it seems like you're trying to get the originating url in a php script, when another one loads first.
One way you could do that is use sessions. Either store the parameters you're trying to extract, and stuff them in a session to be retrieved by the other file, or you could actually just store the url itself, then pull it out and split it.
session_start();
$_SESSION['ourUrl'] = $_SERVER["REQUEST_URI"];
// do stuff on next page
unset($_SESSION['ourUrl']);
session_destroy();
If none of this makes sense feel free to explain further and we'll see if we can get you going. Hopefully this helps a little.