How to store a class in a cookie and retrieve?

Discussion in 'ASP General' started by yootaeho@gmail.com, Mar 22, 2007.

  1. Guest

    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
     
    , Mar 22, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
     
    Anthony Jones, Mar 22, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. angus
    Replies:
    2
    Views:
    476
    Elliot M. Rodriguez, MCSD
    May 20, 2004
  2. Gert
    Replies:
    0
    Views:
    967
  3. Karl
    Replies:
    3
    Views:
    5,220
    saloniSona
    Oct 7, 2009
  4. Replies:
    3
    Views:
    216
    Alessandro Zifiglio
    Aug 2, 2006
  5. Richard  Lawrence

    HTTP::Cookie won't store sent cookie

    Richard Lawrence, Mar 17, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    194
    Richard Lawrence
    Mar 21, 2005
Loading...

Share This Page