Sunday, October 11, 2009

Reading and Writing Browser Cookie in Flex


A browser cookie is a small piece of information sent by a web server to a web browser to be stored for future use. The data in the browser cookie will be sent back to the web server whenever the browser reconnects to the web site.

Cookies are commonly used to store user preference information, such as web site options. Cookies are also used to store shopping cart contents. The most security-relevant use of browser cookies is when they are used to store authentication data, such as user names and passwords.

To Read and write Cookie from a flex application we will use Javascript


//Following function will set the cookie in the user browser
function setCookie(c_name,value,expiredays)
{

value = document.getElementById('user').value;
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}




//Following function will return Cookie
function getCookie(c_name){

if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1 ;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end));
}
}

return "";
}

For reading a cookie Visitor we will use the following line in Flex code:-
var visitorId : String = ExternalInterface.call("getCookie","Visitor");


Cheers
Varun

No comments:

About Me