www.webdeveloper.com
Results 1 to 5 of 5

Thread: Help with cookie program

  1. #1
    Join Date
    Nov 2008
    Posts
    7

    Help with cookie program

    I need help with my script I can't seem to get it working. Can someone please help? I need a cookie script that stores the date and time of your last visit.


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Last Visit</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" href="js_styles.css" type="text/css" />
    </head>
    <body >
    <script type="text/javascript">
    /* <![CDATA[ */
    if (document.cookie) {
    var cookieString = decodeURIComponent(document.cookie);
    var cookieArray = cookieString.split("; ");
    var lasVisit =
    cookieArray[0].substring(cookieArray[0].indexOf("=")
    + 1, cookieArray[0].length);
    document.write("<p>Your last visit was " + lastVisit
    + "</p>");
    }
    else
    document.write("<p>This is your first visit.</p>");
    var now = new Date();
    var day = now.getDay();
    var date = now.getDate();
    var year = now.getFullYear();
    var month = now.getMonth(0);
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    var days = new Array();
    days[0] = "Sunday"; days[1] = "Monday"; days[2] = "Tuesday";
    days[3] = "Wednesday"; days[4] = "Thursday"; days[5] = "Friday"; days[6] = "Saturday";
    var thisVisit = days[day] + " " + month + "/" + date + "/" + year + " at "+ hours + ":" + minutes + ":" + seconds;
    document.cookie = encodeURIComponent(thisVisit);
    /* ]]> */
    </script>
    </body>
    </html>

  2. #2
    Join Date
    Feb 2008
    Posts
    59
    Hi wahboo
    Have a look at this article for reuseable cookie functions which may make things a bit easier for you.
    http://www.astral-consultancy.co.uk/...doco.cgi?11180
    Hope this helps

  3. #3
    Join Date
    Dec 2008
    Location
    Didim, Turkey
    Posts
    105
    Hi wahboo,

    This seems to be a simple solution...

    <script type="text/javascript">

    if (document.cookie)
    {
    document.write("<p>Your last visit was "+unescape(document.cookie)+"</p>");
    }
    else
    {
    document.write("<p>This is your first visit.</p>");
    }

    function setCookie()
    {
    document.cookie=escape(new Date());
    }

    onunload=setCookie;

    </script>

    Mike

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by mikem View Post
    Code:
    if (document.cookie) 
    {
      document.write("<p>Your last visit was "+unescape(document.cookie)+"</p>");
    }
    This will display all the cookies visible on the current domain, which may not be quite what's required.
    Code:
    function setCookie()
    {
      document.cookie=escape(new Date());
    }
    This will create an incorrectly formatted cookie that will expire when the browser is closed.

    This topic is easily researched.

  5. #5
    Join Date
    Dec 2008
    Location
    Didim, Turkey
    Posts
    105

    More with Cookies

    Hi Logic Ali,

    I only said the previous script seems to be a simple solution.
    Of course it can be extended with a 1-year cookie life and cookie name-matching with something like this...

    Code:
    if(document.cookie.match(new RegExp("LastVisit=([^;]+)"))) 
    {
      document.write("<p>Last Visit:"+unescape(RegExp.$1)+"</p>"); 
    }
    else { document.write("<p>This is your first visit.</p>"); }
    
    function setCookie()
    {
        var expiry=new Date();
        expiry.setTime( expiry.getTime() + (365*24*60*60*1000) );
        document.cookie="LastVisit="+escape(new Date())+";expires="+expiry.toGMTString()+";";
    }
    
    onunload=setCookie;
    Mike

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles