2013-10-13 82 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(); 
}