A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: [Resolved] Using ActionScript + PHP to connect to a remote server

  1. #1
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87

    Using ActionScript + PHP to connect to a remote server

    Hi.

    I'm trying to implement an "expire date" checking script in my demo .swf files. I'm sure that the users will have access to internet because of the nature of the file I'm giving for "test drive" to them.

    I would like to create an action script, which would pass identification parameters to a php script located on MY server, which in turn would check the validity of .swf by checking the "valid until" date stored in some file on MY server.

    Second question I have, would you recommend using a mySQL database or a simple file (something like dates.dat) to store the expire date information? I know that mySQL will improve the access speed for multiple users simultaneously verifying their .swf files, but it is not an issue since only a limited number of users will have the access to my .swf.

    Thank you in advance!

  2. #2
    If it is a read-only file, doing it the flatfile way is better than the db way. Reading from a file is definitely much faster than making a connection to a db for a data.

  3. #3
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Thanks for your recommendation. So we're down to one question only:

    Does anyone know any tutorials or have some sample scripts handy which make Flash and PHP talk?

  4. #4

  5. #5
    Member
    Join Date
    Jun 2003
    Posts
    60

    not sure but

    I have done interaction between PHP and one of my flash games eons ago for holding player scores (so that players could have the feeling of competition).

    well, that was long ago as i said. right now i'm at work with no access to flash so the best suggestion i can give is look into the loadVars object in the AS dictionary.

    I'm going to try writing some simple AS blindfolded, so moderators, don't kill me! okay here we go.
    Code:
    var varHolder = new loadVars();
    varHolder.load("http://someserverorother.com/gamescores.php?gameID=russianroulette");
    
    varHolder.onLoad = function(){
    _root.scoreTextBox = varHolder.scores;
    }
    explanation:

    Okay, so say for example you have a php file called gamescores.php, that returns all the scores in one single variable called "scores" given the game ID (in this case, russianroulette). so what the PHP file will do is print something like

    scores=12,13,100,12,14&players=pl1,meowingcat,rhar rr,conan,harrypotter

    on the resulting document. what flash loadVars does is parses these variables into their own vars in the loadVars object, thus you can call the variable as such: myloadvarsobject.<whatevervariable>

    more info on loadVars() here:
    http://www.macromedia.com/support/fl...ionary427.html

    you will notice that loadVars() gives you tools to send commands to the PHP document to talk with the db/flatfile for you

    note: you will notice they mention the loadVariables function... i believe that's be deprecated, but then what can i say, i'm n00b

    hope that helps!
    Last edited by thurinus; 08-17-2004 at 02:35 PM.

  6. #6
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Thanks for the fast reply!

    Ok, now I see how to make flash ask for variables from a PHP file.

    But I'm really a noob in PHP so could you please supply the gamescores.php so I could see how it receives the "gameID" variable and how it sends the "scores" and "players" variables.

    Thanks again.

  7. #7
    Member
    Join Date
    Jun 2003
    Posts
    60
    haha okay, well, that php file i provided is fictitious... but sure, i can quickly write something that (hopefully) works in its fundamental state. are you opting for DB or the flatfile method?

  8. #8
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Thank you for your time.

    I'm going for flatfile in this case, but I'm sure I'll have to use the DB method sometime.

    Anyway, I'll learn PHP and mySQL communication later .

  9. #9
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    thurinus, I got it working. Some trial and error + PHP documentation = Results!

    Sorry that you had to spend your time writing the example.php (while at work!!) for me for nothing.

    Thanks anyway!

    I'll post how I did it in a couple of minutes.

  10. #10
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Here is a simple mechanism to make Flash + PHP talk with simple file access capabilities.

    In flash I created a file named "test.fla"

    On the main timeline I created a dynamic textbox and named it "txtVerify" and a button named "btnVerify".

    Then, to the button I attached the following code:

    code:

    on (release){
    var varHolder = new LoadVars();
    varHolder.load("http://mysiteaddress/validate.php?flashKey=12345");

    varHolder.onLoad = function(){
    _root.txtVerify.text = varHolder.verify;
    }
    }



    Now let me explain what that script does.

    The "varHolder.load" method simply passes the variable "flashKey" which is immediately assigned the value "12345".

    After that "varHolder.onLoad" is called when the "validate.php" supplies the confirmation that the file is valid by passing "verify" variable with value of either "1" or "0" (1 = valid, 0 = invalid).

    Next, I opened up notepad and created a file named "validate.php".

    In this file I put the following code:

    code:

    <?php
    $file = fopen("keys.dat", "r");
    $key = fread($file, filesize("keys.dat"));
    fclose($file);
    if ($key == $flashKey){
    $response = "&verify=1";
    } else {
    $response = "&verify=0";
    }
    print $response;
    ?>



    Firstly, this script will open file "keys.dat" (which will be stored on the server) for read only. Please note, prior to testing this script make sure that you have created "dates.dat" in notepad and put some numbers in it, such as "12345" without quotes as I did in my example.

    After that, PHP reads "keys.dat" to the end and stores it in the string "$key".

    Now "$key" has "12345" stored in it.

    Then, the script compares the flash supplied "$flashKey" with "$key" variable.

    If the keys match, "&verify=1" is stored in the "$response" string.

    Finally, "print $verify;" outputs the contents of "$verify" string into stream, and thats where flash's "varHolder.onLoad" gets called and the textbox gets assigned the value of "verify" which PHP supplied.

    Then I uploaded "verify.php" and "keys.dat" to my server via FTP to some directory named "test" (note that this will be the directory you would have to specify in your flash file when using "varHolder.load"). Also, I checked that the "Public" has a "Read" permission for this folder, or the PHP would not be able to read from "keys.dat"

    Hope this helps other people with the same kind of problem as me.

    And remember, Internet is your best friend!

    Cheers thurinus for showing me the correct path to follow!

  11. #11
    Member
    Join Date
    Jun 2003
    Posts
    60
    awesome job and you're welcome.

  12. #12
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    New Info:

    If your ".swf" will be hosted on other server than the one containing the PHP script, then you need to do the following:

    Create a "crossdomain.xml" file in notepad and put the following in it:
    code:

    <?xml version="1.0"?>
    <!-- crossdomain.xml -->
    <cross-domain-policy>
    <allow-access-from domain="www.externalSiteAccessingData.com" />
    </cross-domain-policy>



    Instead of "www.externalSiteAccessingData.com" you need to put the address of the site on which the ".swf" requiering validation will be stored.

    Then upload this file to the root directory of your site (the site where the PHP script and "keys.dat" are stored)

    For example, if your site where the "keys.dat" is stored is named "www.blah.com" then the "crossdomain.xml" file should be accessible as "www.blah.com/crossdomain.xml".

    After you do this, flash files located on "www.externalSiteAccessingData.com" (which you already changed to proper address) will be able to access the scripts and files located on your "www.blah.com" site.

    Please note, this does not apply to users who open your .swf files from their computers! (meaning they downloaded your .swf file and are opening it from their hard drive)

  13. #13
    Member
    Join Date
    Jun 2003
    Posts
    60
    wow jeez, you're more productive than i am! That is something i've never seen before. thanks! was there any particular source from which you gathered this info?

  14. #14
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Actually, I tried searching Flash MX 2004 help for "LoadVars.load()" documentation, and thats where I saw a solution to my problem (cross domain data exchange).

    So I read the whole thing and found this:
    If you want to load variables from a different domain, you can place a cross-domain policy file on the server hosting the SWF file that is being accessed. For more information, see About allowing cross-domain data loading
    The "About allowing cross-domain data loading" is the link which brought up the info regarding "crossdomain.xml" and Flash security features.

    You are welcome. Hope this helps everyone!

    Edit: Also, here is a site which talks about this: http://www.moock.org/asdg/technotes/...inPolicyFiles/

  15. #15
    Junior Member
    Join Date
    Oct 2006
    Posts
    3

    Can All This be done in asp.net

    Hi All,

    I am new to this forum and to flash also.
    Can someone present a way to do the above thing in asp.net.
    I tried to invoke an aspx url from flash like

    this.createEmptyMovieClip("logo_mc", this.getNextHighestDepth());
    // creates a child movie clip inside of "mc_1"
    // this is the movie clip the image will replace
    logo_mc.createEmptyMovieClip("container_mc",0);
    //logo_mc.container_mc.loadMovie("http://localhost/mediatoflv/a.jpg");
    trace("ASD")
    logo_mc.container_mc.loadMovie("http://localhost/mediatoflv/default.aspx");


    The code that goes in default.aspx page is


    private void Page_Load(object sender, System.EventArgs e)
    {
    FileStream f1 = new FileStream
    (@"http:\\localhost\mediatoflv\a.jpg",FileMode.Ope n, FileAccess.Read);
    byte[] b = new byte[f1.Length];
    f.Read(b,0,(int)f1.Length);
    Response.Write(b);
    }


    But the aspx page is not invoked at all.

    Can anyone please help me.

    Thanks
    Ajit.

  16. #16
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Hi ajitpar,

    Two possible problems with this (please note that I'm not really good with ASP):
    1) You need to make sure you send the appropriate jpeg response header from inside your aspx page (I'm not sure how to do this in ASP, so you'll have to google this up).
    2) Flash might be checking the extension at the end of the URL (such as making sure it's jpeg or swf) and might just ignore the request if it doesn't end with jpeg or swf. Again, I'm not really good with ASP but there might be a way to make ASP parse files ending with jpeg or swf so you can "fake" it and serve dynamic content instead. I'm not sure whether this will break normal images though.

  17. #17
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Just found the correct way to send the headers with ASP here:

    http://www.codeproject.com/aspnet/CaptchaImage.asp

    Code:
    // Change the response headers to output a JPEG image.
    this.Response.Clear();
    this.Response.ContentType = "image/jpeg";

  18. #18
    Junior Member
    Join Date
    Oct 2006
    Posts
    3

    WebServiceConnector Class

    Thanks for that reply.

    I am facing a new and unique problem.
    I am using the WebServiceConnector component to connect to a web service and fetch data. When i run the swf file or when i run the application using CTRL+ENTER ,the appliation runs fine and connects to the webservice to fetch data.
    But when the same swf is hosted under iis inside a web page, I am not able to connect to the webservice.

    Is Something missing out.

    buttonClick
    {
    trace("Button Clicked");
    bebservice.trigger();
    }

    On WebServiceResult
    {
    trace("Got Result");
    }

  19. #19
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Please make sure you have added the domain that will be hosting your Flash file to crossdomain.xml. Take a look a 5-6 posts above. This might be more complicated that that and I won't be able to provide an answer for you as I stopped working with Flash a year ago (the original post I made was in 2004)...

  20. #20
    Junior Member
    Join Date
    Oct 2006
    Posts
    3
    Thanks for the reply.
    I have solved that by placing crossdomain.xml.


    I am facing another problem.
    I recieve an image from this webservice in form of base64binary which i decode using Base64.as class.
    Now how should i display this in flash.
    I am not able to display this data in flash.

    I know that i can write an aspx page to convert this image to a file and then display that in flash(I could do that). But this approach has over head(some internal issues) and i cannot use it.

    Rather can i use the converted base64binary data( I have a base64.as class that encodes and decodes data) somehow to display in flash.


    Any pointers.

    Thanks in Advance
    Ajit
    Last edited by ajitpar; 10-13-2006 at 06:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center