Is there anyway to pass session data from PHP to an .swf?
I know that info derived from GET or POST can be sent to the flash code, but what about session data?

    yes you can retrieve session data and import it into flash. depending on how your session stuff gets handled, it maybe necessary to send the session ID to flash.

    if you have a flash application that's hosted in a PHP page and assuming you are using PHP's basic session functions, you can get the PHP session id into your flash movie by adding the session id to the SWF reference like this:

    <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
     codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
     WIDTH="550" HEIGHT="425" id="newjukebox" ALIGN="">
     <PARAM NAME=movie VALUE="newjukebox.swf?sid=<?=session_id() ?>"> <PARAM NAME=quality VALUE=medium> <PARAM NAME=bgcolor VALUE="#000000"> <EMBED src="newjukebox.swf?sid=<?=session_id() ?>" quality=medium bgcolor="#000000"  WIDTH="550" HEIGHT="425" NAME="newjukebox" ALIGN=""
     TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
    </OBJECT>
    

    Then in the first frame of your flash movie in actionscript, you can refer to the session id:

    // actionscript
    trace(sid);
    

    finally, when you want to load your session data into your flash movie, you are going to have to use a loadvars object in the flash movie. i think that PHP is sometimes smart enough to use the same session for a PHP page and the flash movie embedded in it, but not always. this is why i've described the whole session id stuff above.

    the issue is fairly complicated because PHP has an INI setting enable-trans-sid which means that PHP will attempt to automatically propagate the session ID in a url but will also hide the session id through url rewriting so that it doesn't show up in a user's browser where it could be inadvertently pasted into an email or something and shared with a hacker.

    at any rate, your loadvars code should probably look something like this:

    // actionscript
    var result_lv:LoadVars = new LoadVars();
    result_lv.onLoad = function(success:Boolean) {
      if (success) {
        trace("connect successful");
        trace(result_lv.var_from_php_session);
      } else {
        trace("Error connecting to server.");
      }
    };
    var send_lv:LoadVars = new LoadVars();
    send_lv.name = "Filo T. Farnsworth";
    send_lv.sendAndLoad("http://www.yourdomain.com/mypage.php?PHPSESSID=" + sid, result_lv, "POST");
    

    or something like that. you will probably have to experiment a bit.

    further complicating matters is the fact that your php installation may be configured to use a different variable name for the session id. you can see what the varname is in the results of phpinfo();

    or, sometimes, you can see the varname by running this code:

    echo SID;
    

    the var SID is sometimes defined and sometimes not depending on whether PHP was able to find the session id in a cookie. if you open a blank browser window and visit a php page with that code in it, it should echo something like PHPSESSID=ad87da87d87ad872

    If you refresh the page and you have cookies turned on, it should output nothing at all.

    complicating matters are the various browser types. firefox will remember that session id in a cookie open as long as ANY windows are still open. you could have 20 windows open and visit that page and until you closed EVERY SINGLE browser window, you could return to that php page and see nothing at all because the session id was still stored in a cookie.

    IE on the other hand, all you have to do is open a blank browser window by double-clicking the IE icon on your desktop and it would have no visibility to the cookie.

    anyway, this is probably too much info. sessions can be tricky. try experimenting.

      Great! That answers my question perfectly. Thanks for your insight.

        Write a Reply...