Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Once a day access script problem

    I wanted a script where the user veiws the page once and might see a link that says, "Continue". But the second time the user veiws the page, the text will change to something like, "Sorry you may only access this page once per day. Please return tommorow." The next day the text will reset and read, "continue" again.

    I went to another forum and asked the same questions and helpfully they returned a few scripts.

    The first of which will work in my situation quite fine although it isn't exactly what I wanted. In this script, you press the link 'Try this' for the first time and an alert box pops up and says, "Sorry, but you can only visit this page once a day. Try again tomorrow." and that's what it always has done.
    Here it is:
    Code:
    <html>
    <head>
    <title>Demo Box</title>
    <script language="javascript">
    function ConfirmChoice() 
    { 
    answer = alert("Sorry, but you can only visit this page once a day. Try again tomorrow.")
    if (answer !=0) 
    { 
    location = "index.html" 
    } 
    }
    </script>
    </head>
    <body>
    <div align="center"><a href="#" onclick=" ConfirmChoice(); return false;">Try this!!! </a> </div>
    </body>
    </html>
    The second of the scripts I do not have the knowledge or ability to describe in detail the problems, although I do know that it does not work.... but maybe you know more about how it works and what the problem is. Usually, when I insert the correct pages into the script, it always goes to the page that the user is only supposed to see once a day.
    Here it is:
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }
    function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) 
    break; 
    }
    return null;
    }
    function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (2 < argc) ? argv[2] : null;
    var path = (3 < argc) ? argv[3] : null;
    var domain = (4 < argc) ? argv[4] : null;
    var secure = (5 < argc) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
    }
    function DisplayInfo() {
    var expdate = new Date();
    var visit;
    expdate.setTime(expdate.getTime() +  (24)); 
    if(!(visit = GetCookie("visit"))) 
    visit = 0;
    visit++;
    SetCookie("visit", visit, expdate, "/", null, false);
    var message;
    if(visit == 1) 
    alert("Welcome!")
    if(visit>= 2) 
    alert("You've been here too many times. Try again tommorrow.")
    window.location="about:home"
    
    }
    function ResetCounts() {
    var expdate = new Date();
    expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
    visit = 0;
    SetCookie("visit", visit, expdate , "/", null, false);
    history.go(0);
    }
    
    window.onload=DisplayInfo
    </Script>
    Please keep in mind that I am the very most basic kind of begineer there is to Javascripts (I only got Javascript for dummies yesturday) so I can't edit these scripts myself (thus my post, here at CodingFurums).

    Let's say to keep things organized, we will refer to the too existing problem scripts as script 1 and script 2.

    Also, because of the way my page is set up, users will not be able to refresh the page that the once a day access script will be on.

    Furthermore, any completely new scripts and script ideas are completely welcome.

    Thanks in advance!
    Last edited by Twilight92; 06-03-2006 at 04:54 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an idea: forget about that whole concept (though I'm curious what type of information your visitors can view only once a day).
    The easiest implementation is to use cookies to track visitors, that is what the second script you posted does. However I can easily clear cookies or disallow web site setting them in the first place.
    You could have a server side database and track user IP address and last access time, but then there are dynamic IP addresses and proxies.

    In any event, my point is that you can not reliably accomlish what you are trying to do.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body onload="f19_GetFormCookie();">
    
    <script language="JavaScript" type="text/javascript">
    <!--
    // Form Compendium f19_Part2 (12-05-2005)
    // Form Cookie
    // by Vic Phillips http://www.vicsJavaScripts.org.uk
    
    
    var f19_Days=2;       // The cookie will be available on revisits for a specified number of days
    var f19_Cookie='My Form';        // The Cookie name
    
    var zxcToDay=new Date();
    
    // Functional Code
    
    // No Need To Change ***************************
    
    
    var f19_Exp=new Date(new Date().getTime()+f19_Days*86400000).toGMTString();
    
    function f19_GetFormCookie(){
     zxcCodeDate=(zxcToDay.getFullYear()+'')+(zxcFormatNu(zxcToDay.getMonth())+'')+(zxcFormatNu(zxcToDay.getDate())+'');
     f19_TBString=f19_GetCookie(f19_Cookie);
     if (f19_TBString){
      if (zxcCodeDate<=f19_TBString){
       alert('Go Away');
      }
     }
     f19_SetFormCookie(zxcCodeDate);
    }
    
    function f19_GetCookie(name) {
     var f19_st=document.cookie.indexOf(name+"=");
     var f19_len=f19_st+name.length+1;
     if ((!f19_st)&&(name != document.cookie.substring(0,name.length))) return null;
     if (f19_st==-1) return null;
     var f19_end=document.cookie.indexOf(";",f19_len);
     if (f19_end==-1) f19_end=document.cookie.length;
     return decodeURI(document.cookie.substring(f19_len,f19_end));
    }
    
    function f19_SetFormCookie(value){
     document.cookie=f19_Cookie+"="+encodeURI(value)+";expires="+f19_Exp+";path=/;"
    }
    
    function zxcFormatNu(zxcnu){
     if (zxcnu<10){ zxcnu='0'+zxcnu; }
     return zxcnu;
    }
    
    //-->
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's good, but is there a way so that when the 'Go away' alert box pops up, after the user clicks ok, he/she are redirected to another page?

  • #5
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nevermind! Thanks so much! Thats just what i needed!


  •  

    Posting Permissions

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