简体   繁体   English

将变量从PHP传递到AS3

[英]Passing variable from PHP to AS3

Here I'm passing session value to AS3 but I don't want to send any other thing or content of the page like HTML code and on the other side I don't want to user see the session:x on the page. 在这里,我将会话值传递给AS3,但是我不想发送任何其他内容或页面内容(如HTML代码),另一方面,我也不想让用户在页面上看到session:x

<?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>

Update: 更新:

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

Depends how your AS3 code looks, you can add parameter to your query to distinct it from user and use it to detect it in PHP: 取决于您的AS3代码的外观,您可以向查询中添加参数以使其与用户区分开,并使用它在PHP中进行检测:

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

And in your code: 在您的代码中:

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

After echo() you can use exit() to stop executing php/html code. echo()您可以使用exit()停止执行php / html代码。

Communication between PHP and AS3 is something like the following. PHP和AS3之间的通信类似于以下内容。

First, initiate a session in php and output the swf flash object in html. 首先,在php中启动会话并在html中输出swf flash对象。 In your case, I don't think it is necessary to pass session variable to the object. 在您的情况下,我认为没有必要将会话变量传递给该对象。

<?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>

Second, inside the swf flash object, the AS3 load a php script that will return only name-value pair if it was called from AS3 (notice the fromFlash query string). 其次,在swf Flash对象内,AS3加载一个php脚本,如果从AS3调用该脚本,它将仅返回名称/值对(请注意fromFlash查询字符串)。

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

Third, in speaking.php , output only string with containing the necessary name-value pairs. 第三,在speaking.php ,仅输出包含必要的名称-值对的字符串。 You might need to urlencode the value. 您可能需要对值进行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>

Finally, the AS3 extract data from the called speaking.php. 最后,AS3从调用的Speech.php中提取数据。 Notice the sessionVar variable is the same as the output above echo "sessionVar=" . $_SESSION['myusername']; 注意, sessionVar变量与echo "sessionVar=" . $_SESSION['myusername'];上方的输出相同echo "sessionVar=" . $_SESSION['myusername']; 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM