2013-10-13 112 views
1

在這裏,我將會話值傳遞給AS3,但我不想發送任何其他東西或頁面內容,如HTML代碼,另一方面,我不希望用戶看到會話:x將變量從PHP傳遞到AS3

<?php 
session_start(); 
$session = $_SESSION['myusername'] ; 
if(!isset($_SESSION['myusername'])){ 
    header('location:../login.html'); 
} else{ 
echo "session:".$session; 
header('location:speaking.html'); 
} 
?> 
<html> 
<!-- some HTML code--> 
</html> 

更新:

var sesname:String; 
var loader : URLLoader = new URLLoader(); 
var req:URLRequest = new URLRequest("http://localhost/speaking.php"); 
loader.dataFormat = URLLoaderDataFormat.VARIABLES; 
loader.load(req); 
loader.addEventListener(Event.COMPLETE, connectComplete); 
function connectComplete(event:Event):void{ 
    var session:String = event.target.data; 

    sesname= session; 
    trace(sesname); 
    nextFrame(); 
} 
+0

輸出您的意思是PHP的*檢索*數據,而不是通過* *從PHP? – ihsan

+0

@ ihsan。當用戶登錄時,我需要他的用戶名才能發送到AS3。只有他的用戶名不是頁面中的所有內容之前或之後的回聲。我不知道是否有正確的AS3或不。 – Amir

+0

是這個命令'var req:URLRequest = new URLRequest(「http://localhost/speaking.php」);'調用上面的php代碼?要從url請求中檢索數據,您必須只返回一個變量 - 值對字符串,例如。 '<?php echo「var1 = val1&var2 = val2」?>' – ihsan

回答

0

取決於你的AS3代碼的外觀,您可以向查詢到不同的從用戶添加參數,並用它來檢測它在PHP:

urlVars = new URLVariables(); 
urlReq.data = urlVars; 
urlVars.as3 = 1; 

並在您的代碼:

} elseif($_GET['as3']) { 
    echo "session:".$session; 
    header('location:speaking.html'); 
} 

echo()之後,您可以使用exit()停止執行php/html代碼。

+0

因此,我會失去我的HTML!但我需要它。 – Amir

0

PHP和AS3之間的通信如下所示。

首先,在PHP中啓動一個會話並在html中輸出swf flash對象。在你的情況下,我不認爲有必要將會話變量傳遞給對象。

<?php 
session_start(); 
// some other codes 
?> 
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" id="myFlashMovie" align="middle"> 
    <param name="movie" value="myFlashMovie.swf" /> 
</object> 

其次,SWF Flash對象裏面,AS3加載一個PHP腳本,將只返回名稱 - 值對,如果它是從AS3調用(注意fromFlash查詢字符串)。

var req:URLRequest = new URLRequest("http://localhost/speaking.php?fromFlash=1"); 

三,在speaking.php中,只輸出包含必要名稱 - 值對的字符串。您可能需要urlencode的值。

<?php 
session_start(); 
$session = $_SESSION['myusername']; 
if(!isset($_SESSION['myusername'])) { 
    header('location:../login.html'); 
} else { 
    if (isset($_GET['fromFlash']) && $_GET['fromFlash'] == 1) { 
     echo "sessionVar=" . $_SESSION['myusername']; 
     exit; 
    } else { 
     echo "session:".$session; 
     header('location:speaking.html'); 
    } 
} 
?> 
<html> 
<!-- some HTML code--> 
</html> 

最後,AS3從被叫的speaking.php中提取數據。請注意0​​變量同上echo "sessionVar=" . $_SESSION['myusername'];

// this is the same swf object in step 2 
var req:URLRequest = new URLRequest("http://localhost/speaking.php?fromFlash=1"); 
loader.addEventListener(Event.COMPLETE, connectComplete); 
function connectComplete(event:Event):void{ 
    var variables:URLVariables = new URLVariables(); 

    trace(variables.sessionVar); 
    nextFrame(); 
}