通过ASP在Flash中妙用Cookie
发表于:2007-06-30来源:作者:点击数:
标签:
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 d
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 adv
antage 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 a
lready 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.
原文转自:http://www.ltesting.net