Cookies help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin

    Cookies help

    Hi

    How can I create a session cookie ? I want to create a cookie in browser's
    memory that automatically expires when I close/exit the browser. A need also
    to know how can I access that cookie.

    Thanks in advance for your help

  • kaeli

    #2
    Re: Cookies help

    In article <booeku$1h7eg4$ 1@ID-135637.news.uni-berlin.de>,
    nomail@hotmail. com enlightened us with...[color=blue]
    > Hi
    >
    > How can I create a session cookie ? I want to create a cookie in browser's
    > memory that automatically expires when I close/exit the browser. A need also
    > to know how can I access that cookie.
    >
    > Thanks in advance for your help
    >
    >[/color]

    Have my cookie functions.

    /* This file contains cookie functions. */
    /* File Functions:
    1. setCookie - writes cookie
    2. getCookie - gets value of cookie
    3. removeCookie - deletes a cookie
    4. detectCookies - checks if cookies are enabled
    */

    function setCookie(cooki eName, cookieValue, expireDate)
    {
    /* Pass in three strings - the name of the cookie, the value, and the
    expire date.
    Pass in a "" empty string for expireDate to set a session cookie
    (no expires date).
    Pass in any other date for expire as a number of days to be added
    to today's date. */

    if (expireDate == "")
    {
    expires = "";
    }
    else
    {
    expires = new Date();
    expires.setDate (expires.getDat e() + expireDate);
    expires = expires.toGMTSt ring();
    }
    document.cookie = cookieName+"="+ cookieValue+";e xpires="+expire s;
    }

    function removeCookie (cookieName)
    {
    /* Pass in the name of the cookie as a string and it will be removed.
    */
    expires = Now();
    document.cookie = cookieName+"= ;expires="+expi res.toGMTString ();
    }

    function getCookie (cookieName)
    {
    cookieValue = ""
    if (document.cooki e.indexOf(cooki eName) == -1)
    {
    // there is no cookie by this name for this user
    return cookieValue;
    }
    else
    {
    // get the beginning index of the cookie by looking for the cookie
    name
    cookieStart = document.cookie .indexOf(cookie Name);
    // get the beginning index of the cookie value by looking for the
    equal sign after the name
    cookieValStart = (document.cooki e.indexOf("=", cookieStart) + 1);
    // get the end index of the cookie value by looking for the semi-
    colon after the value
    cookieValEnd = document.cookie .indexOf(";", cookieStart);
    // if no semi-colon, then use the whole length
    if (cookieValEnd == -1)
    {
    cookieValEnd = document.cookie .length
    }
    // use substring to get the text between the two indices and that
    is the value of the cookie
    cookieValue = document.cookie .substring(cook ieValStart,
    cookieValEnd);
    return cookieValue;
    }
    }

    function detectCookies()
    {
    /* function returns true if cookies are enables, false if not */
    setCookie("test ", "test", "");
    tmp = getCookie("test ")
    if (tmp != "test")
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    --
    -------------------------------------------------
    ~kaeli~
    Jesus saves, Allah protects, and Cthulhu
    thinks you'd make a nice sandwich.


    -------------------------------------------------

    Comment

    Working...