I have an application developed in flash, and I need to access some php files. so the php file return some data if the access is came from swf. How can i identify if the request came from flash or not?
without passing get/post variables to php.
User agent/Referer possibly. Keep in mind that requests can easily be forged
I don't think there really is a reliable way of detecting whether Flash made the request. Flash doesn't allow you to set the user-agent, and there are a lot of restrictions on what headers can be set.
Take a look at http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/URLRequestHeader.html
as John Ballinger suggested, you could set your own header using this and look for that header in the PHP page.
This is in response to John Ballinger's answer:
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://www.mydomain.com/myapp.php");
var header:URLRequestHeader = new URLRequestHeader("custom-header-name", "value");
request.requestHeaders.push(header);
try {
loader.load(request);
} catch (error:Error) {
trace("Unable to load requested document.");
}
You must also make sure to modify your crossdomain.xml to allow http headers as follows:
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*.mydomain.com" />
<allow-http-request-headers-from domain="*.mydomain.com" headers="*" />
</cross-domain-policy>
You cannot tell that it is coming from flash as flash actually uses the browser to do the request.
But in your flash request you could add your own header to the HTTP request (you can do this pretty easily in flash). That way you can see if the request is coming from Flash.
Related
I have created a form using Flash & actionscript 3. The problem I am having is that I can send the variables to php when stored at the same location as that of the swf file, but if i want to make an android AIR application for the form, I cant address the php file which send me the email. Is it possible to address a remote php file? Please help me, I am stuck at this stage.
Probably you stuck with crossdomain.xml.
The idea that first flash load the policy file and check's permissions: can flash access to this server or not. It's a simple security: to prevent flash doing request's where it shouldn't.
For more information just google by keyword crossdomain.xml.
I found the solution. I just had to use the following code.
var url:String = "php url";
var my_url:URLRequest = new URLRequest(url);
my_url.method = URLRequestMethod.POST;
my_url.data = my_vars;
var my_loader:URLLoader = new URLLoader();
my_loader.dataFormat = URLLoaderDataFormat.VARIABLES;
my_loader.load(my_url);
So i am trying to communicate between dart clientside and a php server side using AJAX. Since direct execution is not possible. I compiled the dart to javascript and then run it on a apache server.
json data is generated at client end, But there is no response from the the server
dart code
import 'dart:html';
import 'dart:json';
void main() {
query("#clicker").on.click.add(callServer);
}
void callServer(Event event) {
var data ={ 'name':"sendname"}
,jsondata=stringify(data);
print(jsondata);
var req = new HttpRequest();
req.open('post','http://localhost:8080/darttest/server.php',true);
//req.setRequestHeader('Content-type','application/json');
req.send(jsondata);
print(req.responseText);
}
php side i just echo the content received
<?php
$name = $_POST['name'];
echo $name;
?>
This is my first try at dart programming, so do let me know if this approach is even possible
Is localhost:8080 serving both the static Dart (as JS), and the php? If not, you're likely coming across the access-control-allow-origin issue (which is a browser security issue).
This prevents one site posting date to another site.
Work-arounds:
Ensure that the site serving php returns the correct CORS headers: http://enable-cors.org/server.html
Serve the static Dart/JS files from the same URL (localhost:8080)
For more information, read these:
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
CORS with Dart, how do I get it to work?
Creating a bot/crawler
Update Workaround 3 is described here (for Chrome / Dartium):
https://groups.google.com/a/dartlang.org/d/msg/misc/kg13xtD7aXA/uxeXXrw3CG8J
You can add the parameter "--disable-web-security" to chrome.exe to disable cross domain check.
(Of course, this is only useful while you are developing)
To read the response, you have to put your code in a callback on readyStateChange :
var req = new HttpRequest();
req.open('post','http://localhost:8080/darttest/server.php',true);
req.on.readyStateChange.add((e){
if (req.readyState == HttpRequest.DONE && req.status == 200){
print(req.responseText);
}
});
req.send(jsondata);
With your code the http request was not processed when you tried to read the response. You have to wait the completion of the request to read the response.
this is not sending data between dart and php. this is sending data from dart to php!!!
I write my scripts in PHP, and there are HTML and javascripts inside. What I want is when I click a button(in HTML), it calls a javascript function, the function should visit a url like "http://localhost/1/2" And the page stays as before. Is it feasible?
I just want it work, no matter in js or php. Thanks.
Since the page is on the same domain, you may use an Ajax request:
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.send(null);
Note that this does not do any error-checking, however. If you need that, there are a multitude of available tutorials easily found with a search.
And since you ask, for pages not on the same domain, using an <iframe> is sometimes possible:
var frame = document.createElement("iframe");
frame.src = url;
frame.style.position = "relative";
frame.style.left = "-9999px";
document.body.appendChild(frame);
This is commonly known as AJAX (being able to send a request to the server and receive a response back without navigating away from the page).
AJAX is supported in ALL modern browsers, but sometimes there are inconsistencies, so it is best to use a javascript framework such as JQuery, YUI or another framework.
I tend to use YUI, so here's a quick example on how to send an AJAX request using YUI. This uses the IO Utility:
// Create a YUI instance using io module.
YUI().use("io", function(Y) {
var uri = "http://localhost/1/2";
// Define a function to handle the response data.
function complete() {
Y.log('success!');
};
// Subscribe to event "io:complete"
Y.on('io:complete', complete);
// Make an HTTP request to 'get.php'.
// NOTE: This transaction does not use a configuration object.
var request = Y.io(uri);
});
So here's the deal,
I'm trying to save variables from flash to PHP, however when i try to do so the php script seems to not acknowledge that i'm logged in.
This would mean that the session doesn't arrive in flash. How do i fix this?
AS3 added:
var bitmap:BitmapData = new BitmapData(loaded_swf.width, loaded_swf.height - 273, false, 0x000000);
var saveSelfyRequest:URLRequest = new URLRequest('urlhere.php');
saveSelfyRequest.contentType = "application/octet-stream";
saveSelfyRequest.method = URLRequestMethod.POST;
var bytes:ByteArray = PNGEncoder.encode(bitmap);
requestLoader = new URLLoader();
saveSelfyRequest.data = bytes;
requestLoader.load(saveSelfyRequest);
requestLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
I don't know Flash or ActionScript, but once I had a similar problem while developing a Java Applet which interacts with a php application which requires login. The problem was the fact that the applet make independent requests to the web app and not through the browser, so it was not passing a proper session id to the php app.
I solved this problem by passing a session_id parameter to the applet (using a param tag) and then making post requests to the app by setting a cookie with the value PHPSESSID=xxxxx in the HttpClient used for the request.
Maybe you can try the same inside ActionScript.
To get confirms on my theory you can print all received headers in the php application to see if the cookie header is sent in the request.
I am having some consistency problems with my flash application, when I echo out variables for flash to get, it doesn't always pick up what PHP is sending, it seems to vary from PC to PC.
I am getting info from a database, and I need to pass it to flash, say for instance I need to send through 5 variables $uid,$name,$points,$from,$page , how could I go about sending these from PHP to flash using AMFPHP?
I was told that AMFPHP would be the best tool to use for such situations, but I have NO knowledge of how it works and the sample code on the site is not making complete sense to me.
Thanx in advance!
You cannot push it from PHP to Flash - the communication has to be initiated by the Flash end. And you don't need AMFPHP for this; just use a URLLoader.
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest("page.php"));
function onLoad(e:Event):void
{
var loadedText:String = URLLoader(e.target).data;
/**
* Following will throw error if the text
* is not in the format `a=something&b=something%20else`
* */
var data:URLVariables = new URLVariables(loadedText);
for(var t:Object in data)
trace(t + " : " + data[t]);
}
inside the page.php, just do a simple echo:
//don't forget to urlencode your variables.
echo "uid=$uid&name=$name&points=$points";
It seems a hassle to get involved with AMFPHP just to send a couple of variables to a flash file. I suggest you try:
Flashvars (though it's kind of limited to short variables)
LoadVariables
XML (returning the values you need as XML from PHP)
All of the above have worked consistently for me.