A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Weird Behavior with URLLoader + PHP Echo

  1. #1
    Junior Member
    Join Date
    Jan 2009
    Posts
    15

    Weird Behavior with URLLoader + PHP Echo

    Hi experts. I'm having a bit of trouble using URLLoader with PHP echo.

    Inside my PHP script I have a session variable which I echo out. If I simply view this in a browser the value looks similar to :

    output of $token in browser
    195421653823333|2.AQF1F_Pv6fkFj1GR.3200.1312754300 .0-24233010|JdZ5WMGbo65OyRikrKAMqRmW6Co

    output is always empty in the flash trace....?

    The weird thing is that if I do :

    $token = "195421653823333|2.AQF1F_Pv6fkFj1GR.3200.131275430 0.0-24233010|JdZ5WMGbo65OyRikrKAMqRmW6Co";

    explicitly in the php script the output does come back to flash properly? My code is below...


    PHP code:

    <?php session_start();
    $token = $_SESSION["user"]["auth_token"];

    echo $token;

    ?>

    AS3: Code
    function getToken():void{
    var request:URLRequest = new URLRequest (currentHost + "flash/getToken.php");

    var loader:URLLoader = new URLLoader (request);
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.dataFormat = URLLoaderDataFormat.TEXT;
    loader.load(request);
    var token:String;

    function onComplete(evt:Event):void{
    token = evt.target.data;
    trace(token);
    loader.removeEventListener(Event.COMPLETE, onComplete);
    }
    }

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    when you echo you have to use a name value pair something like

    PHP Code:
    echo "varName=test"
    where varName is the name of the variable that it parses and test is the string data that is its value.
    ~calmchess~

  3. #3
    Junior Member
    Join Date
    Jan 2009
    Posts
    15
    I'm confused... wouldn't that only be true if I was using:

    loader.dataFormat = URLLoaderDataFormat.VARIABLES?

    and also encoded the response?

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    u right i didn't look I just thought you were using Variables......pardon me are you only loading text ? Please tell me what data you are loading.
    ~calmchess~

  5. #5
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I'll get back to you in a minute I have to look in the live docs
    ~calmchess~

  6. #6
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    i looked and you really need to use Variables with PHP the text attribute is used to read a text file something like myText.txt
    ~calmchess~

  7. #7
    Junior Member
    Join Date
    Jan 2009
    Posts
    15
    Alright... can you please provide an example ? of how I would do that?

  8. #8
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    This is the flash side

    PHP Code:
    package LoadVars0{
        
    import flash.display.*;
        
    import flash.events.*;
        
    import flash.net.*;
        
    import flash.text.*;
        
        public class 
    LoadVars0 {
            

            public function 
    LoadVars0() {
             var 
    loader:URLLoader=new URLLoader();
             
             
    loader.addEventListenerEvent.COMPLETEhandleComplete);
             
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,handleSecurity);
             
    loader.dataFormat URLLoaderDataFormat.VARIABLES;
             
             
    loader.load( new URLRequest("http://mydomain.com/theinfo.php"));
              
            }
            private function 
    handleCompleteevent:Event):void{
                var 
    loader:URLLoader URLLoaderevent.target );
                 switch(
    loader.dataFormat) {
                    case 
    URLLoaderDataFormat.TEXT :
                   
                        break;
                    case 
    URLLoaderDataFormat.BINARY :
                        
                        break;
                    case 
    URLLoaderDataFormat.VARIABLES :
                      
                        
    trace(loader.data.username);
                        break;
                }

                var 
    test loader.data.username;
                var 
    test1 loader.data.ip;
                
    //trace("username = " + test1);
            
    }
            private function 
    handleSecurity():void{
                
    trace("security error");
            }
            
        }


    And this is the simplest PHP

    PHP Code:
    echo "username="BOB"&"ip=192.168.1.1"; 
    PS. if the php doesn't work you may need to add & sign just before "username .........If i remember right you don't want it in an AS3 loader but u do need it in an AS2 loader.
    ~calmchess~

  9. #9
    Junior Member
    Join Date
    Jan 2009
    Posts
    15
    Hi, Thank you for your help with this. Unfortunately, none of that solved my problem. After more research I found that session variables cannot be echoed back to flash since flash assigns a different session id...Therefore, my solution was to store the session variable in a mysql database and then use php to retrieve the value. Then it properly came back to flash....

    But... now I have another problem... the variable contains the special character "|" as shown above. And so when flash reads this from the echo it shows this has been converted to "&" .. How can I make it so it properly returns "|" ??

  10. #10
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    you can use session variables hold on i will get you some code
    ~calmchess~

  11. #11
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I set my session data with html and PHP and then call this PHP script with URLLoader to load user name.......don't break into mysite. LOL j/k

    PHP Code:
    <?php session_start();

    if(!isset(
    $_SESSION['guname'])){
    $username=$_SESSION['IxuG07EZ'];
    echo 
    "user=$username";
    }else{
    $domain=$_SERVER['REMOTE_ADDR'];
    $ran0=rand(1,5);
    $stlen=strlen($_SESSION['guname'])/$ran0;
    $sub0 =substr($_SESSION['guname'],0,6);
    $sub1 =substr($_SESSION['guname'],-$stlen,4);
    $username=$sub0.$sub1;
    echo 
    "user=$username&ip=$domain";
    }

    ?>
    ~calmchess~

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