频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> asp教程 -> 通过ASP在Flash中妙用Cookie

通过ASP在Flash中妙用Cookie

作者:未知  来源:转载  发布时间:2005-9-17 21:15:34  发布人:acx

减小字体 增大字体

Written by: Günter Hoffellner
Translated by: Bernhard Spuida
First published: 9/8/2000

Setting or reading cookies in Flash may be necessary for example to extend the personalization of a web
site to the Flash file.

Flash does not support direct setting and reading of cookies. Thus, you either have to take the often
published detour of using Javascript or you just use ASP scripts to set and read cookies. This has among
other things the advantage that Flash can access cookies even when Javascript is disabled.

The Flash File
In the following the Flash file which can read and set cookies at the client is described. The file calls
the ASP scripts testCookies.asp, setCookies.asp and getCookies.asp to gain access to the browser's cookies
via ASP.

The Flash file tests for the permission to set client side cookies and allows entering data that is to be
stored in a cookie. The file also reads the cookie content and displays it on screen.

There are two files in the download: one .fla in English, and a second one in German. The compiled .swf is
available in German only.

The User Interface
The user interface is divided into three parts which are described in the following sections.


Figure 1: User interface in Flash

Step 1:
The user clicks the 'START TEST' button to trigger the test on the server to determine whether the browser
of the page visitor accepts cookies. The status message indicates whether the browser accepts cookies or
not.

Step 2:
In the case of cookies being accepted, the Flash movie runs on to the second part and waits for data to be
saved in a cookie. With a click on the 'SEND DATA' button, the data is transmitted to the server.

Step 3:
In the last part, the server passes the cookie data back to the Flash file where they are written into the
text fields.

The scripts of the Flash File
The graphic part of the Flash file is built according to standard procedure and is not described in detail
for this reason.

The layer with the name 'Sourcecode' is important, as this contains the source code of the Flash file. We
will go into the details of this now.


Figure 2: Time line in Flash


//Frame 1
Set Variable: "cookies" = "false"
stop


The variable 'cookies' is initialised with the string 'false'. 'false' is used as the server does not
return the boolean values true and false, but a string with the value of "true" or "false". Flash waits
for the button click for starting the cookie test.


//Frame 2, Label step1
Load Variables ("/testcookies.asp", 0)


The file 'testcookies.asp' is called and returns 'true' or 'false' (Cookies accepted - Cookies not
accepted).


//Frame10
If (cookies eq "true")
Set Variable: "cookietest" = "Cookies can be set"
Stop
Else
Set Variable: "cookietest" = "Cookies not allowed. Please enable."
Go to and Stop ("nocookies")
End If


In Frame 10 a different message is displayed in the status field depending on the result of the cookie
test.

If cookies are not accepted, Flash jumps to the label 'nocookies', stops there and will not accept any
input but another cookie test. In the if-condition cookies eq "true" is set in quotes as the server does
not really return the boolean values of true or false, but a string which is immediately tested as such.
Also note that the string comparison requires 'eq' instead of '='.

The 'stop' command forces Flash to wait for a click event of the 'SEND DATA' button. Sending the cookie
data to the server happens as follows:


//Frame11, Label step2
Load Variables ("/setcookies.asp?cookiename="&name&"&"&
"cookiemail="&email&"&"&"cookietelephone="&telephone, 0)


In this script, a query string containing the user entries is sent to the file ' setcookies.asp'. For the
composition of the query string, refer to the article 'Data Exchange between ASP and Flash' (German only).


//Frame19, Label step3
Load Variables ("/getcookies.asp", 0)

The data read from a cookie by ASP is loaded into the Flash file.
//Frame 28
Stop


The loaded data is displayed in the text fields after a short animation of a line.


//Frame 35, Label nocookies
stop


If the test in frame 10 determines that no cookies are allowed, the Flash time line branches to this frame
and the user has the opportunity to perform another test.

The scripts of the ASP files
The 3 files testcookies.asp, setcookies.asp and getcookies.asp are called by Flash for the following
actions: Checking whether the browser accepts cookies (testcookies.asp), setting cookies (setcookies.asp),
reading cookies(getcookies.asp).

Checking whether the browser accepts cookies
The file testcookies.asp checks whether the browser permits cookies. The technique is the same as in the
article "Simple Browser Cookie Test" (German only). The variation of this script used here is as follows:


<%
strTest = Request.QueryString("CookieTest")
If UCase(strTest) <> Ucase("true") Then
' First call
' Set session variable
Session("__FlashCookieTest") = True
' Redirect with QueryString
strURL = Request.ServerVariables("script_NAME")
strQueryString = "?CookieTest=true"
Response.Redirect(strURL & strQueryString)
Response.End
Else
' Redirect already happened
' Check whether the session variable contains the value
If Session("__FlashCookieTest") = True Then
' Session variable contains value
' Thus browser accepts cookies
strOut = "Cookies=true"
Else
' Session variable is empty
' Thus browser does not accept cookies
strOut = "Cookies=false"
End If
End If
' Output to Flash:
Response.Write(strOut)
%>


Simply put, the script sets a session variable, performs a redirect onto itself and then checks whether
the value still is set in the session variable. For this value to stay saved, the browser must have
accepted the session cookie of the Internet Information Server (IIS) which is automatically sent to the
browser by ASP. This means that the browser accepts cookies when the value in the session variable is
still present after a redirect.

The file returns the text 'Cookies=true' if the browser accepts cookies or 'Cookies=false' if the browser
does not accept cookies.

When Flash calls the file testcookies.asp via the Flash command Load Variables, the ASP file returns
either the string "Cookies=true" or "Cookies=false" to the Flash file which automatically sets the
variable Cookies to "true" or "false" in Flash.

Setting Cookies
In ASP, cookies can be set easily using the Response.Cookies collection. In our example, the Flash file
calls the file setcookies.asp and passes the variables for the cookies (names and values) in the
Querystring to the ASP script.

The ASP script reads all values out of the Querystring collection sends the corresponding cookies via
Response.Cookies to the browser. The complete ASP script consists of only three lines:


<%
For each item in Request.QueryString
Response.Cookies(item) = Request.Querystring(item)
Next
%>


Reading Cookies
Reading the cookies and writing them to the Flash File is about as easy as setting the cookies. When the
Flash file calls the file getcookies.asp, it returns all the names and values of the cookies as URL-
encoded text. This way, the values of the cookes are written to variables of the same names in Flash when
Flash calls the filegetcookies.asp.

The ASP file getcookies.asp is as follows:


<%
For each cookie in Request.Cookies
strOut = strOut & Server.URLEncode(cookie) & "="
strOut = strOut & Server.URLEncode(Request.Cookies(cookie))
strOut = strOut & "&"
Next
Response.Write strOut
%>


Conclusion
The Flash based part of this article was created using Flash 4. The systematics are the same for the newly
published Version 5 of the Macromedia software.

The ASP part runs on IIS 4.0 as well as on IIS 5.0 and with slight modifications also under ASP+.

After having described setting and reading of cookies in Flash in conjunction with ASP in this article,
one of the next articles will further develop this by using the present base for simple personalization of
a Flash file using cookies.


将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 优化MySQL数据库性能的八..
· 如何在PHP中进行身份认证..
· 掌握部署 Access项目的方..
· asp性能测试报告(转)(..
· PHP--进行模块化设计
· 快讯:和讯总编透露近期..
· WWW服务器讲解 (3)
· Dreamweaver MX进阶教程..
· 当php程序中需要客户窗口..
· 用perl访问mysql数据库之..
· flash action 详解
· 免费论坛联盟,免费申请个..
· PS精通:羽毛的制作的最新..
· 互联网巨头中国高管集体..
· 在WebPart上创建控件
· photoshop解决高难度抠图..
相关文章
· JavaScript静态页面值传递:..
· 在PHP网站开发中如何使用co..
· Java应用技巧:对于 Cookie..
· Java小技巧:关于Cookie的操..
· 如何制作PHP中的Cookies?
· 动态网页技术PHP关于cookie..
· ASP.NET 1.1 无 Cookie Ses..
· ASP 跨越域的Cookie(2)
· ASP 跨越域的Cookie(1)
· Java Servlet及Cookie的使用..
· 用Cookie实现仅弹出一次窗口..
· 请看用javascript设置和读取..
· 客户端Cookie中文编程(1)
· 客户端Cookie中文编程(2)
· 跨越域的Cookie
· 通过ASP与ACCESS数据库建立..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号