using session values

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

    using session values

    I have done alot of web work in all sorts of languages but I have never used
    the simple cookie. Question is
    How does one set and retrieve a simple session cookie. All it will do is
    have an image change on 1 page only with every new visitor, there are 5
    images it should be random and stay the same unless the session is ended.
    Sounds pretty simple but I cannot find simple examples of a cookie doing
    anything similar to this. I found oodles of stuff on what a cookie is and
    how it works but I could use an example, even if someone knows a website
    that has simple clear examples, and to prove my ignorance what prog.
    language are they written in. I have always used variables associated with a
    db in asp but asp is not an option so it looks like html or JavaScript ????

    Here is a simple image rotator but how would you set the cookie and check to
    see if it is a new session for the rotator?


    <SCRIPT LANGUAGE="JavaS cript">
    image = new Array(3);
    image[0] = 'images/bodymidrow1col2 .jpg'
    image[1] = 'images/bodymidrow1col2 _2.jpg'
    image[2] = 'images/bodymidrow1col2 _3.jpg'
    index = Math.floor(Math .random() * image.length);
    document.write( "<DL>\n");
    document.write( "<IMG SRC="+image[index]+" height=120>");
    </SCRIPT>

    J
  • kaeli

    #2
    Re: using session values

    In article <8d3b05e8.04041 50254.33c03b33@ posting.google. com>,
    stop_usingmynam e@yahoo.co.uk enlightened us with...[color=blue]
    > I have done alot of web work in all sorts of languages but I have never used
    > the simple cookie. Question is
    > How does one set and retrieve a simple session cookie.[/color]

    A session cookie is merely one that has no expiration set, thus doesn't
    really save itself to the cache.
    So, the same way one uses any cookie, but don't set an expires time.

    Here is my cookies file for JS. Have fun.
    (watch for word wrap)

    /* jsCookies.js */
    /* 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, expireDays)
    {
    /* 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 (expireDays== "")
    {
    expires = "";
    }
    else
    {
    expires = new Date();
    expires.setDate (expires.getDat e() + expireDays);
    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~
    If that phone was up your a$$, maybe you could drive a
    little better!



    Comment

    Working...