使用 Flash Actionscript 设置 PHP 会话变量 [英] Setting PHP session variables using Flash Actionscript

查看:34
本文介绍了使用 Flash Actionscript 设置 PHP 会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从我的 Flash 应用程序调用的简单 PHP 上传脚本.我确定它会调用,因为它实际上是上传文件!

I have a simple PHP upload script that is called from my Flash App. I am sure it makes the call because it actually uploads the file!

session_start();

$default_path = 'files/';

$target_path = ($_POST['dir']) ? $_POST['dir'] : $default_path;

if(!file_exists($target_path)) mkdir($target_path, 0777, true);

$destination = $target_path . basename( $_FILES[ 'Filedata' ][ 'name' ] );

$file_name =  rand(1,9999).$_FILES[ 'Filedata' ][ 'name' ];

if(move_uploaded_file($_FILES[ 'Filedata' ][ 'tmp_name' ], $destination)){

$_SESSION['path'] = 'flashuploader_online/upload/'.$destination;

}

但是,我尝试在另一个脚本中使用会话变量path",但它给了我一个空值!是的,我已经确保使用 session_start.

However, I try to use the session variable "path" in another script but it gives me an empty value! Yes, I have made sure to use session_start.

我错过了什么吗?

至少现在我知道问题是什么了!但我不知道如何解决它,而不会因为传递会话变量而变得混乱.有什么想法吗?

At least now I know what the problem is! But I am not sure how to solve it without it getting messy to pass across session variables. Any ideas?

推荐答案

您将不得不通过将 session_id 作为变量传递来在所有请求中持久化它.我保证不会太乱!您需要对显示 Flash 的页面以及它发布到的脚本进行一些更改.您还需要对 Flash 应用程序本身稍作更改,以便在将文件上传到服务器时包含会话 ID.

You are going to have to persist the session_id across all requests by passing it as a variable. I promise it won't get too messy! There are a couple changes you will need to make to the page that displays the flash as well as the script it posts to. You will also need to make a slight change to the Flash application itself, so that it can include the session ID when it uploads the file to the server.

首先,您需要通过将会话 ID 包含在 FlashVars 中来为 Flash 提供会话 ID.您将需要使用 PHP 对显示 Flash 的页面进行预处理,否则将无法保持会话.确保在输出 Flash 的页面中调用 session_start().你最终会得到这样的结果:

First, you will want to provide flash with the session ID by including it with FlashVars. You're going to need the page that is displaying the flash to be preprocessed with PHP, or it will not be possible to persist a session. Make sure you call session_start() in the page that outputs the Flash. You'll end up with something like this:

<object classid="clsid:(blah)" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="800" height="800" id="ZoomifyHotspotViewer">
  <param name="flashvars" value="phpsessionid=<? print session_id(); ?>">
  <param name="src" value="YourSWF.swf">
  <embed flashvars="phpsessionid=<? print session_id(); ?>" src="YourSWF.swf" pluginspage="http://www.adobe.com/go/getflashplayer" type="application/x-shockwave-flash" width="800" height="800" name="YourSWF"></embed>
</object>

特别是这部分是需要在param和embed标签中添加的:

This part in particular is what needs to be added, in both the param and embed tags:

phpsessionid=<? print session_id(); ?>

然后,在您的 Flash 应用程序中,当您发出请求时,您现在可以访问变量phpsessionid"中的会话 ID.您需要在名为 PHPSESSID(全部大写)的 POST 变量中包含该值 - 包含它,但是您要包含其他变量,例如您使用的dir"变量.

Then, in your Flash app when you make the request, you will now have access to the session id in the variable 'phpsessionid'. You need to include the value in the POST variable named PHPSESSID (all caps) - include it however you are including your other variables such as the 'dir' variable you make use of.

包含该变量将确保当您在下一页调用 session_start() 时,将恢复会话而不是启动新会话.有几种配置情况不会自动发生这种情况.如果事实证明您是这种情况(即下一页上的会话 ID 仍然不同),您需要在处理上传的页面中执行以下操作:

Including that variable will ensure that when you call session_start() on the next page, the session will be restored instead of a new session being started. There are a couple configuration cases where this doesn't happen automatically. If that turns out to be the case for you (i.e. the session id is still different on the next page), you need to do the following in the page that processes the upload:

session_id($_POST['PHPSESSID']);
session_start();

这将手动强制 PHP 更新具有指定 ID 的已保存会话.这甚至不应该是您必须处理的问题,但如果是,您可能必须在下一页上做类似的事情,用户也将继续这样做,或者向所有页面添加一般情况:

This will manually force PHP to renew the saved session with the specified ID. This shouldn't even be an issue you have to deal with, but if it is you may have to do something similar on the next page the user continues to as well, or add a general case to all pages:

if (isset($_REQUEST['PHPSESSID'])) {
  session_id($_REQUEST['PHPSESSID']);
}
session_start();

确保如果您最终需要以这种方式作为 setter 调用 session_id(),请在调用 session_start() 之前这样做.

Be sure that if you do end up needing to call session_id() in this way as a setter, that you do so before calling session_start().

这篇关于使用 Flash Actionscript 设置 PHP 会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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