在Flash AS3中加载PHP URL [英] Loading PHP URL in Flash AS3

查看:166
本文介绍了在Flash AS3中加载PHP URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Flash AS3的在线游戏,并使用mySQL数据库的PHP服务器。我正在使用PHP操纵mySQL数据库中的数据,当我直接从浏览器请求PHP文件时,数据库完全改变。我有以下AS3代码:

pre $ public $ getSite(string):Boolean {

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


/ *
我们使用URLRequest方法获取我们的php文件的地址并附加php变量。
* /

var urlRequest:URLRequest = new URLRequest(string);

/ *
这里使用了POST方法,所以我们可以使用php的$ _POST函数来接收我们的php变量。
* /

urlRequest.method = URLRequestMethod.POST;
$ b $ *
这将我们的php变量附加到url请求
* /

urlRequest.data = phpVars;
$ b $ *
我们使用URLLoader类将请求URLVariables发送到php文件
* /

var urlLoader:URLLoader = new URLLoader );
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE,check(t));
t = check(t);
$ b $ * b $ b运行函数,一旦php文件已经对闪存
* /

/ *
发表了请求PHP文件
* /

urlLoader.load(urlRequest)
return t;



$ b函数check(t:Boolean):函数{
返回函数(event:Event):Boolean {
trace (event.target.data.checkResult);
if(event.target.data.checkResult ==Good){
t = true;
} else {
t = false;
}
return t;




$ b现在从这里开始,我的 trace 显示URL已加载,输出为Good,但数据库值不会更改。这是PHP文件:

 <?php 
/ *
连接到我们的数据库
* /
include_onceconnect.php;
$ sql =更新帐户SET PlayersOnline = accounts.PlayersOnline + 1;
$ query = mysql_query($ sql)或退出(checkResult = Bad);
exit(checkResult = Good);
?>

当我到'localhost / php / gameSearch.php' code>在我的网页浏览器中数据库的变化,我想知道是什么问题。

解决方案

您有一个缓存问题。换句话说,已经请求的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.

这篇关于在Flash AS3中加载PHP URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆