Sending a POST through flash, shows not logged in on PHP? - php

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.

Related

Load remote php to send form submission variables, is it possible?

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

How to check remote server for status

I'm looking for something.
The problem is that I want to make my app do more locally and less remote.
It needs to put a parameter in every request (or websession id)
I tried the following
$.getJSON('http://*******************.com/loginapi.php?uuid=0x1a2b3c4e', function(jd) {
var reg = jd.usrreg;
var uuid = jd.usrpin;
var usrid = jd.usrid;
});
});
That does nothing, on the server side I don't even see a request to that page.
document.write(uuid) does not give anything back, when I go in my browser to the requested page I see the following:
{"usrreg":"0","usrpin":"0x1a2b3c4e","usrid":"0"}
I also tried with form data, so when I press login it sends a request to the server, but I should still get something back like usrreg=0 or 1, because it means the person is not registered, or usrreg=2 for wrong user/pass.
How can I read the value that I get when I open the page?
Cross Domain Request with Ajax are fobidden cause of Same Origin Policy: http://en.wikipedia.org/wiki/Same_origin_policy
So what are you alternatives?
Learn to use AJAX with JSONP - will only work if the other side provides it
Write a PHP bridge running locally that provides the remote data for you
Use postMessage: https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage

Visit a URL in PHP or javascript in background

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);
});

How do i know if the request came from flash swf?

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.

PHP/Flash integration inconsistencies

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.

Categories