how many users online, PHP

Discussion in 'PHP' started by Javver, Feb 1, 2010.

  1. #1
    is there a way to display how many users are online in the site in PHP without utilizing mysql?
     
    Javver, Feb 1, 2010 IP
  2. radikaalis

    radikaalis Peon

    Messages:
    113
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    yeah, the easiest way is to use pre-made scripts or services offering that. Im using http://whos.amung.us/ in my blog and are very glad with that.
     
    radikaalis, Feb 1, 2010 IP
  3. Javver

    Javver Active Member

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Thanks, let me check that out.
     
    Javver, Feb 1, 2010 IP
  4. puppetguy

    puppetguy Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    create a blank file and name it online.txt


    create a file online.php and add the below code to it.

    Upload to your server and give online.txt write permissions.
     
    puppetguy, Feb 3, 2010 IP
  5. Javver

    Javver Active Member

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    thank you guys!
     
    Javver, Feb 19, 2010 IP
  6. boi4

    boi4 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use scripts and be happy :D
     
    boi4, Feb 20, 2010 IP
  7. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hello puppetguy
    I tried your code, but when i refresh page it shows 0 useronline
     
    Om ji Kesharwani, Feb 21, 2010 IP
  8. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Why not just create a session on page load, and assign $_SESSION["user"] to "Guest" or another anonymous name. Then to display Who's Online just count the session array. Haven't tried this method first hand (never had a reason to show this type of data) but I don't see why it wouldn't work.

    All in all it would be about 3 lines of code.
     
    AntelopeSalad, Feb 21, 2010 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    The easiest way to count online users is to create a new $_SESSION for each user and then count the number of files in your "session.save_path" directory.
     
    joebert, Feb 21, 2010 IP
  10. Shakil

    Shakil Greenhorn

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #10
    Thanks for the script, helped me too

    Same problem....

    how can i solve the problem, i m newbie in php, can anybody give the code where it will not show "0 users online" when i refresh the page means it show always same user untill i leave the page.

    plz help....
     
    Shakil, Feb 21, 2010 IP
  11. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #11
    Try this code:
    Copy the code and put in a file, and include that file on all php files where you want to count and show total users.
    Make a ip.txt file to store data

    
    
    <?php
    $file= 'ip.txt'; //file to store data
    $every=20;  //start counting from 0 after these many minutes
    
    
    //no need to edit below
    $ip = $_SERVER['REMOTE_ADDR'];
    $a= file_get_contents($file); $a=explode("\n",$a);
    if(is_numeric($a[0])){
    if((time()-$a[0])>($every*60)){
    $st= time()."\n".$ip;
    $fp=fopen($file,'w'); fwrite($fp,$st); fclose($fp);
    }else{
    if(!in_array($ip,$a)){
    $st= "\n".$ip;
    $fp=fopen($file,'a'); fwrite($fp,$st); fclose($fp);
    }
    }
    }else{
    $st= time()."\n".$ip;
    $fp=fopen($file,'w'); fwrite($fp,$st); fclose($fp);
    }
    echo '<p>Total users online: '. (sizeof($a)-1). '</p>';
    
    ?>
    
    Code (markup):
    Thanks :)
     
    JEET, Feb 22, 2010 IP
  12. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks JEET.
    All subscribers to this thread can apply JEET code its working fine.
     
    Om ji Kesharwani, Feb 22, 2010 IP
  13. Shakil

    Shakil Greenhorn

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #13
    what does it means, plz make me clear.

    Thanks .....
     
    Shakil, Feb 22, 2010 IP
  14. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #14
    It means that "total users online: 50 in last 20 minutes"
    It's tracking total users online in last 20 minutes, and after 20 minutes it deletes all data and starts new tracking. If you want to track for last 5 minutes, use:
    $every=5;

    for the last 1 hour:
    $every=60;

    and so on.
    Thanks :)
     
    JEET, Feb 22, 2010 IP
  15. Shakil

    Shakil Greenhorn

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #15
    Thanks JEET for ur greater help, JEET could u make me some more help...

    1) Problem is to use this script, this script take some times to load it, need more faster script.
    2) Can i use it for my business website?
    3) Can u help me for shortcut? means i want to use a line script not full script every page. Full script will have in another secured page or secured file.
     
    Shakil, Feb 23, 2010 IP
  16. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Write script in a new page say counter.php
    Then call it on the page where you want to show total online visitor
    or just include it in your footer...

    <? include('counter.php') ?>
     
    Om ji Kesharwani, Feb 23, 2010 IP
  17. Shakil

    Shakil Greenhorn

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #17
    thanks to give a shortcut....
     
    Shakil, Feb 25, 2010 IP