欢迎光临
我们一直在努力

通过ASP在Flash中妙用Cookie

建站超值云服务器,限时71元/月

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 browsers 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.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 通过ASP在Flash中妙用Cookie
分享到: 更多 (0)