Home All Groups Group Topic Archive Search About

How to store a class in a cookie and retrieve?

Author
22 Mar 2007 1:15 AM
yootaeho
Hi,
I have the following script
<%@LANGUAGE=Javascript%>
<%
var myClass = new LoginInfo();
  myClass.sessionID = "1321312131";

  Response.Cookies("testingCookies") = myClass;

  var recievedCookies = new LoginInfo();
  recievedCookies = LoginInfo(Response.Cookies("testingCookies"));

  Response.Write(recievedCookies.sessionID);
  Response.End();

function LoginInfo()
  {var sessionID;}
%>

How to make it work?

Cheers

Author
22 Mar 2007 10:00 AM
Anthony Jones
<yoota***@gmail.com> wrote in message
Show quoteHide quote
news:1174526149.581642.155250@p15g2000hsd.googlegroups.com...
> Hi,
> I have the following script
> <%@LANGUAGE=Javascript%>
> <%
>  var myClass = new LoginInfo();
>   myClass.sessionID = "1321312131";
>
>   Response.Cookies("testingCookies") = myClass;
>
>   var recievedCookies = new LoginInfo();
>   recievedCookies = LoginInfo(Response.Cookies("testingCookies"));
>
>   Response.Write(recievedCookies.sessionID);
>   Response.End();
>
> function LoginInfo()
>   {var sessionID;}
> %>
>
> How to make it work?
>
> Cheers
>

You need to enable your object to serialise and deserialise the state of the
object to a string.

For example (by no means a complete implementation and is air code). :-

function LoginInfo(vsStateIn)
{
    var moState = vsStateIn ? eval(vsStateIn) : {}
    vsStateIn = null

    this.getSessionID = function() { return moState.sessionID; }
    this.putSessionID = function(value) { moState.sessionID = value; }

    this.serialise = function()
    {
        var sState = '{'
        for (var key in moState)
        {
            if (sState.length > 1) sState += ', '
            if (typeof(moState[key]) == 'string')
                sState += key + ": '" + moState[key].replace(/([\\|\'])/g,
'\\$1') + "'"
            else
                sState += key + ": " + moState[key].toString()
        }
        sState += '}'
        return sState
    }
}


To store the object:-

var oLogin  = new LoginInfo()
oLogin.putSessionID("1321312131")

Response.Cookies("testingCookies") = oLogin.serialise()  ;


To retrieve the object:-

var oLogin  = new LoginInfo(Request.Cookies("testingCookies").Item)
if (oLogin.getSessionID == "1321312131")
{

    //is logged on

}


For further Ideas google JSON

Bookmark and Share

Post Thread options