我正在尋找一種播放Flash動畫內容的方法(即使用戶導航到具有類似Flash內容的不同HTML頁面),並在設定的時間量或瀏覽器關閉後過期。讓瀏覽器會話在FLASH後共享對象過期?
我知道我可以使用共享對象,但似乎無法找到有關如何清除它們在瀏覽器會話結束的信息。
我打開使用JavaScript或PHP來協助。
您的幫助表示讚賞 - 感謝-MW
我正在尋找一種播放Flash動畫內容的方法(即使用戶導航到具有類似Flash內容的不同HTML頁面),並在設定的時間量或瀏覽器關閉後過期。讓瀏覽器會話在FLASH後共享對象過期?
我知道我可以使用共享對象,但似乎無法找到有關如何清除它們在瀏覽器會話結束的信息。
我打開使用JavaScript或PHP來協助。
您的幫助表示讚賞 - 感謝-MW
而不是使用一個共享對象,你可以創建兩個簡單的服務器端的服務:通過一個生成的XML文件公開會議一個保持會話,以及一個您的Flash應用程序可以使用。
第一個服務會設置一些會話變量,並應在每次播放視頻時調用。它可能看起來像這樣:
<?php
// start-video.php
session_start();
$_SESSION['hasWatchedVideo'] = true;
$_SESSION['watchedVideoAt'] = time();
?>
第二個服務是根據會話生成XML響應的服務。這可能是這樣的:
<?php
// flash-config.php
session_start();
// set expiration to 5 min
define(VIDEO_TIMEOUT, 300);
$playVideo = "true";
if($_SESSION['hasWatchedVideo']
&& (time() - $_SESSION['watchedVideoAt']) < VIDEO_TIMEOUT) {
$playVideo = "false";
}
header("Content-Type: text/xml");
echo "<config><playVideo>{$playVideo}</playVideo></config>";
?>
然後從您的Flash應用程序,你可以這樣做:
/**
* Called whenever the app is loaded.
*/
protected function init():void {
var u:URLLoader = new URLLoader();
u.addEventListener(Event.COMPLETE, onComplete);
u.load(new URLRequest("http://example.com/flash-config.php"));
}
/**
* Determines whether or not the video should play based on the
* config service response.
*/
protected function onComplete(e:Event):void {
var x:XML = new XML(e.target.data);
if(x.playVideo == 'true') {
playVideo();
}
}
/**
* Should be called either when the video starts playing. I just tied
* it to a user click here.
*/
protected function playVideo():void {
// call the service to update the session
var u:URLLoader = new URLLoader();
u.load(new URLRequest("http://example.com/start-video.php"));
// ... play video code ...
}
我覺得這個方法讓你比使用共享對象多一點靈活性。希望有所幫助。
UPDATE:
你可以在瀏覽器中使用的會話cookie,以及。基本上將失效日期設置爲'0',並且cookie將在用戶關閉瀏覽器時過期。 (注意:當我在Firefox中進行測試時,關閉標籤並不足以殺死cookie,整個瀏覽器必須關閉。)
您可以使用ExternalInterface或utility library like this。使用庫,你可以在你的閃存應用程序中有這樣的代碼:
function playVideo():void {
if(!CookieUtil.getCookie('playvideo')) {
CookieUtil.setCookie('playvideo', 'true', 0);
// ... play video code ...
}
}
每當用戶關閉瀏覽器,cookie將被清除。下次他們訪問您的網站時,該視頻將再次播放。不知道這是否與您所尋找的內容更爲接近,但希望它有幫助。
我修改你的代碼一點點,因此將在年底使sesion被殺害......
的PHP ...
<?php
// flash_php_session_cookie.php
$cookie= "false";
if (isset($_COOKIE["cookie"]))
$cookie= "true";
else
setcookie("cookie", "true", 0);
echo "<config><cookie>{$cookie}</cookie></config>";
?>
的FLASH ...
// the folowing functions handle call coresponding PHP files to handle cookies ...
// re-configure these to the location of the swf ...
var flashConfigURL:String ="flash_php_session_cookie.php";
//Called whenever the app is loaded ...
function initCookieFunc():void {
var u:URLLoader = new URLLoader();
u.addEventListener(Event.COMPLETE, onComplete);
u.load(new URLRequest(flashConfigURL));
}
// Determines whether or not the cookie exists/(assumes theres a text field named T on the stage) ...
function onComplete(e:Event):void {
var x:XML = new XML(e.target.data);
if (x.cookie == 'false') {
T.appendText("cookie doesn't exist yet");
} else {
// cookie exists ...
T.appendText("cookie exists");
}
}
initCookieFunc();
我我會保留一個鬆散版本的「TIMEOUT」 版本。 這是偉大的,再有一個答案
感謝RJ的寶貴代碼
-MW
你只需要到期共享對象自己。這並不複雜。 這樣,您的.swf將完全自成體系,不依賴任何外部的IMO是好事。
package {
import flash.display.Sprite;
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
public class SharedObjectExample extends Sprite {
private var _so:SharedObject;
private var _now:Date;
private var _last_played:Number;
private static const EXPIRE_TIME:Number = 1000 * 60 * 60 * 24; // 24hrs in msec
public function SharedObjectExample() {
// create a new date for the current time to compare against
_now = new Date;
// create a new shared object
_so = SharedObject.getLocal("application-name", "/");
// try read from the shared object
if (_so.data.last_played) _last_played = _now;
// if no value is set we play the video and set the current time
if (!_last_played) {
// play video here
_last_played = _now.time;
// check if the "cookie" has expired and it's time to play again
} else if (_now.time - _last_played > EXPIRE_TIME) {
// play video here
_last_played = _now.time;
} else {
// do nothing
}
// and finally, save
saveValue();
}
private function saveValue(event:MouseEvent):void {
// i've removed the code that asks the user for permission if the request for storage is denied
_so.data.last_played = _last_played;
var flushStatus:String = null;
try {
flushStatus = _so.flush(1000);
} catch (error:Error) {
trace("Could not write SharedObject to disk");
}
}
}
}
另外,請注意,您需要使用數字而不是int,因爲涉及的大數將會溢出int – grapefrukt 2010-01-11 10:39:18