www.webdeveloper.com
Results 1 to 6 of 6

Thread: User online state indicator

  1. #1
    Join Date
    Feb 2006
    Posts
    352

    Post User online state indicator

    Good day to you all,
    I working on a login / logout code, now I need to find out who is online and not.

    I was thinking of a javascript which would count time spend on page.

    here it is :
    PHP Code:

    <SCRIPT LANGUAGE="JavaScript">



    <!-- 
    Begin
    startday 
    = new Date();
    clockStart startday.getTime();
    function 
    initStopwatch() {
    var 
    myTime = new Date();
    return((
    myTime.getTime() - clockStart)/1000);
    }
    function 
    getSecs() {
        var 
    tSecs Math.round(initStopwatch());
        var 
    iSecs tSecs 60;
        var 
    iMins Math.round((tSecs-30)/60);
        var 
    sSecs ="" + ((iSecs 9) ? iSecs "0" iSecs);
        var 
    sMins ="" + ((iMins 9) ? iMins "0" iMins);
        
    document.getElementById("timespent").value sMins+":"+sSecs;
        
    window.setTimeout('getSecs()',1000);
    }
    // End -->
    </script>


    <BODY onLoad="window.setTimeout('getSecs()',1)">

    <CENTER>
    <FORM>
    <input size=5 id="timespent" name="timespent">
    </FORM>
    </CENTER> 


    Now I need that for each increment of one minute, a txt file would be update (+1)
    PHP Code:

    <?php
    $File 
    "YourFile.txt";
    $Handle fopen($File'w');
    $Data "Jane Doe\n";
    fwrite($Handle$Data);
    $Data "Bilbo Jones\n";
    fwrite($Handle$Data);
    print 
    "Data Written";
    fclose($Handle);
    ?>

    Also

    If the txt file reach a count of 15, user get logout automatically.


    Can somebody help me include the last 2 point to my javascript code ?

    Thanks !

  2. #2
    Join Date
    Apr 2007
    Posts
    1,664
    And if I want to stay logged in beyond 15 minutes, I just have to disable JavaScript. PHP/SQL is a better solution. Write the login time and session ID into a MySQL table and use a cronjob to run a PHP script to look how long each has been active then after 15 minutes log the user with that session ID out.

  3. #3
    Join Date
    Feb 2006
    Posts
    352
    the php session will kick you out anyway no ?

    I can't use db for this one nor cron job ?

  4. #4
    Join Date
    Apr 2007
    Posts
    1,664
    Quote Originally Posted by Peuplarchie View Post
    the php session will kick you out anyway no ?

    I can't use db for this one nor cron job ?
    No because if a user interacts with the server the session timeout will reset. If you want to kick a user out after 15 mins you need to know when they came in then regularly poll the record to see when 15 mins has passed. JavaScript is way simple to bypass. Also you can't update a textfile on a server unless you use ajax.

    You can do it with a flat file or sqllite if you cant use a DB it's just not as easy.

    How come cron (or schedule task on Windows) isn't an option? If you have a lot of user interaction with your site ie page loads < 1 minute you can just include the monitoring file in a site wide header.
    Last edited by SyCo; 06-30-2009 at 04:46 PM.

  5. #5
    Join Date
    Feb 2006
    Posts
    352
    sorry I do have access to cron job, but I never made one..
    Where can I find a list of command I can use ?

  6. #6
    Join Date
    Apr 2007
    Posts
    1,664
    cronjob looks ridiculously difficult but is really quite simple. Lots of tuts out there to explain it better than me.
    http://www.google.com/search?q=runni...ipts+with+cron

    One way to do it in a flat file is, you can write the username/sessionid to a file
    Code:
    123<split>1231313123123
    234<split>53543453542352
    345<split>afafafawfeawff
    read if with file() explode on the <split> tag and then process it just like you got it from a data base. Rebuild the line entries for the file and re-write it. If it's easier you can put it all on one line if you want and double explode it.

    Code:
    123<split2>1231313123123<split1>234<split2>53543453542352<split1>345<split2>afafafawfeawff
    In that example explode on <split1> and you have an array to loop and explode on <split2> inside the loop to process the info. Call the script with cron every minute or 30 seconds.

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