且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Flash AS3中加载PHP URL

更新时间:2022-11-28 10:20:29

您有一个缓存问题。换句话说,已经请求的URL的结果被缓存,以减少延迟和访问时间,你所代表的是输出的缓存副本,而不是执行的 fresh 输出为了解决这个问题,你可以推一个 no-cache 头部添加到请求对象上的 requestHeaders 属性(该属性的类型为 URLRequestHeader )。然而,运行时看起来是无知的头,它总是提供缓存的副本!

为了克服这个问题,但是,你需要欺骗的运行时间,就像你通过附加一个虚拟随机值变量来请求一个新的URL:

  getSite(localhost / php / file.php R =+的Math.random()); 



关于您提供的特定代码, URLLoader 异步工作,这就是为什么你注册一个完整的监听器! t = check(t); 语句会导致您试图检查结果,而当时可能还没有准备好!您应该在侦听器触发时/之后检查它。除了赋值在语法上是不恰当的(将一个 Function 赋给一个 Boolean !!)并重新考虑逻辑检查函数!

在PHP代码中,正如其他人所建议的,最终不要使用不推荐使用 mysql_query 函数并使用更合适的API。


I am working on an online game in Flash AS3 and utilizing a PHP server with mySQL database. I am manipulating the data in mySQL database using PHP and when I request the PHP file in a browser straightly from 'localhost/php/file.php', the database changes perfectly. I have the following AS3 code:

    public function getSite(string):Boolean{

        var phpVars:URLVariables = new URLVariables();
        var t:Boolean = false;


        /*
        we use the URLRequest method to get the address of our php file and attach the php vars.
        */

        var urlRequest:URLRequest = new URLRequest(string);

        /*
        the POST method is used here so we can use php's $_POST function in order to recieve our php variables.
        */

        urlRequest.method = URLRequestMethod.POST;

        /*
        this attaches our php variables to the url request
        */

        urlRequest.data = phpVars;      

        /*
        we use the URLLoader class to send the request URLVariables to the php file
        */

        var urlLoader:URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
        urlLoader.addEventListener(Event.COMPLETE, check(t));
        t = check(t);

        /*
        runs the function once the php file has spoken to flash
        */

        /*
        we send the request to the php file
        */

        urlLoader.load(urlRequest)
        return t;


}

function check(t:Boolean):Function{
    return function (event:Event):Boolean{
        trace(event.target.data.checkResult);
        if(event.target.data.checkResult == "Good"){
            t = true;
        } else {
            t = false;
        }
        return t;
    }
}

Now from here, my "trace" shows that the URL is loaded and the output is "Good", however the database values does not change. This is the PHP file:

   <?php
   /*
   connect to our database
   */
   include_once "connect.php";
   $sql = "UPDATE accounts SET PlayersOnline = accounts.PlayersOnline + 1";
   $query = mysql_query($sql) or exit("checkResult=Bad");
   exit("checkResult=Good");
   ?>

When I go to 'localhost/php/gameSearch.php' in my web browser the database changes, and I am wondering what the problem is.

You have a "caching" problem. In other words, the result of the already requested URL is cached to reduce latency and access times, and what you've represented is the cached copy of the output and not a fresh output resulting from the execution of the instructions on behalf of the server.

To overcome the issue, you could've pushed a no-cache header to the requestHeaders property on your "request" object (the property is of type URLRequestHeader). However, the runtime looks to be ignorant on the header and it always provides the cached copy!

To overcome that issue, however, you need to fool the runtime as if you are requesting a new URL every time by appending a dummy random-valued variable:

getSite("localhost/php/file.php?r="+Math.random());


And regarding your specific provided code; The URLLoader works asynchronously, that's why you register a "on complete" listener! The t = check(t); statement induces you're attempting to "check" the result while it may not be ready by then! You should check it when/after the listener triggered. In addition to the fact that the assignment is syntactically inappropriate (assigning a Function to a Boolean!) and reconsider the logic of the check function!

And in the PHP code, as others have suggested, ultimatly don't use the deprecated mysql_query function and use a more appropriate API.