www.webdeveloper.com
Page 1 of 6 123 ... LastLast
Results 1 to 15 of 82

Thread: How many users on page

  1. #1
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859

    Question How many users on page

    I need to know how I can setup a php script that would display "There are currently ## users on this site" on my main page of my site and it would be accurate. I was told you need to make it so the user registers but I need to know if thier is an easyer way and if so how and if not how do I do the user registration thing? Any help would be greatly appreciated! Thanks.

  2. #2
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    You could do it with a Java applet and server side script, with JavaScript and Server-side script (although it wouldn't be very accurate), by time using PHP (ask me to explain later, I'm tired, it's bout 10:30 and I've had quite a lot to drink), or by logins. Tomorrow I will make you a script combining JavaScript and timing to do it roughly. I look forward to working with you again.

  3. #3
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    If you want to do it via login + register, that would be better, and I can help you to do that...?

  4. #4
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    Here's a script tht will calculate the number of users on a page at any one time, roughly. (I say roughly because people without JavaScript will never be detected as leaving). Without using login + register I think it's the best you can do. This is the bit where Pyro proves me wrong...

    visitors.php:
    Code:
    <?php 
    $fh=fopen("online.txt","r+");
    $read=fgets($fh,12);
    setType($read,"integer");
    
    if($_GET['minus']) $read--; else $read++;
    rewind($fh);
    fwrite($fh,$read);
    echo("There are currently $read users online");
     ?>
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>Visitor counter</title>
    <?php if(!$minus) echo(' <script type="text/javascript">
    <!--
    function leave() {
    notify=window.open("visitors.php?minus=yes");
    window.close(notify);
    }
    // -->
    </script>');?>  
    </head>
    <body <?php if(!minus)echo('onunload="leave()"'); else echo('onload="window.close()');?> >
    
    </body>
    </html>
    online.txt:
    Code:
    0
    Just add your content to the framework provided by visitors.php. Need any more help, just post.

  5. #5
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859

    Post Thank you Cijori

    Thanks for your response. I was wundering if you could possibly post the files or the link to an example so I could see it in action and see if I like it? I would do it but I usally screw it up pretty bad and I want a working example. You could post both of you ideas the "Non Registration + Login" and the "Login + Registration" one so I could see which one I will use. Thank you so much for your help. I may not respond right away as I'll probably be gone all day so I'll try to view your response as soon as possible. Thanks again for your help.

  6. #6
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    lol I am inturding on the sacred ground of the php forum and I myself do not use php. Anyways to do a login/register you would need login forms and reistration forms and what makes you thing people are going to login if there is nothing for them you see where it says people browsing this forum thats login and registration but because we need to login to post it forces us to login which makes that method accurate but notice it only tells you who is loggedin not who is browsing and it does this by adding your name to a data base then removing it onunload or whatever the php equivalent to that is. It is the same in all server side llanguages the principle atleast not the code.

  7. #7
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859
    Well I was kinda planning to have a login/registation thing any way since I plan to have a chat room and forum and stuff on my site that I will only want registered users to have access to! I think that all I will worrie about is the amount of users logged in so people will know when someones on the forum or chat room or what not! I also want to be able for them to be greeted with their username when they log in on the main page and if they are not logged in it will say guest!

  8. #8
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    well the greet username is not as hard as the currently logged in junk all you have to is grab their session username or whatever the feild in your data base is for user name.

  9. #9
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Originally posted by cijori
    Without using login + register I think it's the best you can do. This is the bit where Pyro proves me wrong...
    What I would do is just check if the users are active. This will count the number of active users in 15 minutes:

    You need a file named data/online.txt, and don't forget to CHMOD it so it is writeable (666)
    PHP Code:
    <?


    $ip 
    $_SERVER['REMOTE_ADDR'];
    $time time();
    $minutes 15;
    $found 0;
    $users 0;
    $user  "";

    $tmpdata $SERVER['DOCUMENT_ROOT']."/online/data";

    if (!
    is_file("$tmpdata/online.txt"))    
        {
        
    $s fopen("$tmpdata/online.txt","w");
        
    fclose($s);
        
    chmod("$tmpdata/online.txt",0666);
        }

    $f fopen("$tmpdata/online.txt","r+");
    flock($f,2);

    while (!
    feof($f))
        {
        
    $user[] = chop(fgets($f,65536));
        }

    fseek($f,0,SEEK_SET);
    ftruncate($f,0);

    foreach (
    $user as $line)
        {
        list(
    $savedip,$savedtime) = split("\\|",$line);
        if (
    $savedip == $ip) {$savedtime $time;$found 1;}
        if (
    $time $savedtime + ($minutes 60)) 
            {
            
    fputs($f,"$savedip|$savedtime\n");
            
    $users $users 1;
            }
        }

    if (
    $found == 0
        {
        
    fputs($f,"$ip|$time\n");
        
    $users $users 1;
        }

    fclose ($f);
    print 
    "<font style='color:white;font-size:10pt'>Visiters Online :&nbsp;$users</font>";
    ?>

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  10. #10
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    god your code is confusuing when you dont know php. look like java script on shrooms.

  11. #11
    Join Date
    Feb 2003
    Location
    Britain
    Posts
    1,335
    The hard part is remembering how to use loads of functions- there are literally hundreds. Guess what Pyro can do that I can't! Hopefully that will come with experience. Pyro, how long have you been using PHP?

  12. #12
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    I've been using PHP for around 6 months or so... As far as remembering functions, php.net is your best friend...

    Personal website http://www.ryanbrill.com/
    Business website: http://www.infinitywebdesign.com/
    TypeSpace http://www.typespace.org/

    I reject your reality and substitute it with my own!

  13. #13
    Join Date
    May 2003
    Location
    Florida
    Posts
    40
    There is a way to count active sessions I do believe...brb with that answer
    hi

  14. #14
    Join Date
    May 2003
    Location
    Florida
    Posts
    40
    Well I ddint find anything in the 5 minutes I searched, but ther are hints that it is possible. You would have to access the global $_SESSION of course. If nothing else, set up a database table and log users from your main page by either there ip address or their GUID and check it on each page they go to also log their sessid and then on sessid_timeout have it remove it

    sorry im in a hurry to get home and play counterstrike lol just got off work
    hi

  15. #15
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859

    Talking Thanks!

    Thank you guys. You all had great answers for my question. Pyro, thanks for your response! I think your idea will work for what I'm trying to accomplish but being such a newb to php that you know I am I need a little more explination on how to impliment this idea if you have time. From what I gather I need to make a php file with the php code you gave me and make a log.txt or something but I need more explination on what goes in the txt file and where I put the php file and the txt file and how I would set up my main html file to display "Currently Active Users". I would greatly appreciate any more help you can provide. Thanks so much for your help and hope to see a response from you soon! Also thanks again to all that responded.

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