簡體   English   中英

在Flash AS3中加載PHP URL

[英]Loading PHP URL in Flash AS3

我正在Flash AS3中開發在線游戲,並在MySQL數據庫中使用PHP服務器。 我正在使用PHP處理mySQL數據庫中的數據,當我直接從'localhost/php/file.php'在瀏覽器中請求PHP文件時,數據庫發生了完美的變化。 我有以下AS3代碼:

    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;
    }
}

現在從這里開始,我的“ trace ”顯示已加載URL,輸出為"Good" ,但是數據庫值未更改。 這是PHP文件:

   <?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");
   ?>

當我在網絡瀏覽器中轉到'localhost/php/gameSearch.php' ,數據庫發生了變化,我想知道問題出在哪里。

您有一個“緩存”問題。 換句話說,已經請求的URL的結果被緩存以減少延遲和訪問時間,並且您所代表的是輸出的緩存副本,而不是代表服務器執行指令而產生的輸出。

為了解決此問題,您可以將“ no-cache標頭推送到“ request”對象上的requestHeaders屬性(該屬性的類型為URLRequestHeader )。 但是,運行時在標頭上看起來是無知的,它始終提供緩存的副本!

但是,要解決該問題,您需要通過附加一個虛擬隨機值變量來欺騙運行時,就像每次請求一個新URL一樣:

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


以及關於您提供的特定代碼; URLLoader異步工作,這就是為什么要注冊“完整”偵聽器的原因! t = check(t); 語句會導致您嘗試“檢查”結果,而此時可能尚未准備好! 您應該在偵聽器觸發時/之后檢查它。 除了該分配在語法上不合適(將一個Function分配給Boolean !)之外,還要重新考慮check函數的邏輯!

正如其他人所建議的那樣,在PHP代碼中,最終不要使用不推薦使用的mysql_query函數,而不要使用更合適的API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM