Advertisement

08.21.2008 at 09:55PM PDT, ID: 23669305 | Points: 500
[x]
Attachment Details

Can't get PHP Session to work with Flash

Asked by ckoning in ActionScript, JavaScript, PHP Scripting Language

Tags: , , , ,

I have a flash application that uses FileReference and URLLoader objects to interface with a php backend. The php structure requires login to lookup user specific data. The user logs in through a php form, which sets session variables to enable this functionality. However, since the previously mentioned AS3 classes do not pass cookie information, the php session id is not sent with the requests. Consequently, the php scripts fail to look up the proper data, and the system fails. I have code to locate the cookie via javascript, parse it for the php session ID, and then pass that to the back end as part of the URLVariables attached to the URLRequest given to the FileReference and the URLLoader. The php script checks for the session id in the URL variables (POST specifically) and then uses that value and the session_id() function to look up and resume the session. In theory, this should enable the php script to find the proper data. Indeed, when the session id is passed using php forms, everything is golden. However, when the actionscript is executed, it not only fails to find the user info, but the session is broken, and the user must log in again to access any session variables in PHP or ASStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
/**
 *
 * AS snippet
 *
 */
// Create a file reference
var file:FileReference = new FileReference();
// Get the session ID from the cookie
var res:String
= ExternalInterface.call('function(){return document.cookie;}');
var sidStr:String = res.substr(10,res.length);	
// Create a new URL Request
var req:URLRequest = new URLRequest();
req.url = "http://mysite.com/php/session.php";
req.method = URLRequestMethod.POST;
var reqvars:URLVariables = new URLVariables();
reqvars.sid = sidStr;
// Upload File (myFileFieldName is the name of the file in $_FILES var)
file.upload( req, myFileFieldName ); 
 
/**
 *
 * PHP snippet
 *
 */
// Get the session ID and start the session
session_id( $_POST['sid'] );
session_start();
// Return the info to the Flash App as XML for parsing
header("Content-type: text/xml");
$xml = new DOMDocument('1.0', 'iso-8859-1');
$child = $xml->createElement("upload");
$data = $xml->createElement("data");
// Returned XML contains the correct session id
$ssid = $xml->createElement("ssid",session_id());
// But not the user information, and session is unset or
// otherwise destroyed/lost/who knows
$info = $xml->createElement("userInfo",$_SESSION['userInfo']);
$data->appendChild( $info );
$data->appendChild( $ssid );
$child->appendChild($data);
$xml->appendChild( $child ); 
echo $xml->saveXML();
exit();
[+][-]08.21.2008 at 11:35PM PDT, ID: 22287356

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.22.2008 at 08:44AM PDT, ID: 22291184

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.22.2008 at 11:10AM PDT, ID: 22292610

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.22.2008 at 11:15AM PDT, ID: 22292657

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628