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
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display time of last visit with login name

    I was trying to display time of last visit along with login name..

    can someone help me on this pls?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,244
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Use a cookie.

    Code:
    <body onload ="readCookie('mycookie'); eraseCookie('mycookie'); createCookie('mycookie', 365)">
    
    <script type = "text/javascript">
    
    function createCookie(name,days) {
    if (days) {
    var date = new Date();
    var value = new Date();  
    date.setTime(date.getTime() + (days*24*60*60*1000));
    var expires = "; expires = "+ date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "="+ value + expires + "; path=/";      // available throughout the directory
    }
    
    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) {;
    retval = c.substring(nameEQ.length,c.length);
    alert (retval);  // for testing
    return retval;
    }
    }
    return null;
    }
    
    function eraseCookie(name) {
    createCookie(name,"",-1);  // set cookie to expire on previous day
    }
    
    </script>
    
    name = "mycookiename" or whatever name you wish to give the cookie
    days = number of days cookie to remain active (say 365)
    The value of the cookie is the date/time of the last visit. var value = new Date(); You can of course manipulate the date object to change the display as required. Example:
    Code:
    var value = new Date().getDate() + "/" + new Date().getMonth()+1 + "/" + new Date().getFullYear();  // DD/MM/YYYY
    Remember that in Javascript months are 0-11

    Of course, cookies can be (and often are) erased by the user.


    Why should I do anything for posterity? What has posterity ever done for me?
    Groucho Marx (1890 - 1977)
    Last edited by Philip M; 05-13-2012 at 02:20 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Location
    Heartland of America, Oklahoma
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is it possible to add to the cookie, something that would ask the question if this is not the original log-in person? For instance, I log-in to my site when the cookie asks for my name - rickself3. If someone else uses my computer to log in to my website, the cookie would say "Hello, rickself3". What would I add to the cookie that would have a "Not rickself3? Click Here"

    Thanks!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    You don't "add to the cookie" for that. You add a place in your HTML that says that and then the cookie is used just to fill in the name.

    Example:
    Code:
    Hello, <span id="name1"></span><br/>
    (If not <span id="name2"></span>, <input type="button" value="click here" onclick="...to code to change namd..."/>
    And then use the JS to just fill in name1 and name2.

    Code:
    var person = readCookie("person");
    document.getElementByID("name1").innerHTML = person;
    document.getElementByID("name2").innerHTML = person;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,244
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Quote Originally Posted by rickself3 View Post
    Is it possible to add to the cookie, something that would ask the question if this is not the original log-in person? For instance, I log-in to my site when the cookie asks for my name - rickself3. If someone else uses my computer to log in to my website, the cookie would say "Hello, rickself3". What would I add to the cookie that would have a "Not rickself3? Click Here"

    Thanks!
    Your computer cannot tell who is actually using it, so anyone else using your machine will receive the "Hello rickself3" message. This is true even with server-side coding.

    You can of course check that the log-on user name and the name stored in the cookie match, and if not display a different message, but the next time that rickself3 logs on then the cookie would contain a different user name and request him to enter his credentials again.

    If your computer has multiple users then it is not practical with Javascript to identify which user is actually using it now. Note that Javascript cannot access the Windows registry so cannot identify which Windows usrname is currently logged on.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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