Have you ever wished to display the number of online users on your PHP powered website or on your WordPress blog without the use use of Plugin and wondered how you could achieve this using PHP tags?

ow to show display online users in PHP

Here is a guide to help achieve displaying the number of online users on your website/blog in past X minutes using my simple PHP code.

How to Show Online Users in PHP SCRIPTS

  • Copy this code below to Notepad and save it as online.php .


    <?php
    //Online Users For PHP Scripts by Oscar Frank of www.oscarmini.com
    $dataFile = "visitors.txt";
    $sessionTime = 1; //this is the time in **minutes** to consider someone online before removing them from our file
    //Please do not edit bellow this line
    error_reporting(E_ERROR | E_PARSE);
    if(!file_exists($dataFile)) {
    $fp = fopen($dataFile, "w+");
    fclose($fp);
    }
    $ip = $_SERVER['REMOTE_ADDR'];
    $users = array();
    $onusers = array();
    //getting
    $fp = fopen($dataFile, "r");
    flock($fp, LOCK_SH);
    while(!feof($fp)) {
    $users[] = rtrim(fgets($fp, 32));
    }
    flock($fp, LOCK_UN);
    fclose($fp);
    //cleaning
    $x = 0;
    $alreadyIn = FALSE;
    foreach($users as $key => $data) {
    list( , $lastvisit) = explode("|", $data);
    if(time() - $lastvisit >= $sessionTime * 60) {
    $users[$x] = "";
    } else {
    if(strpos($data, $ip) !== FALSE) {
    $alreadyIn = TRUE;
    $users[$x] = "$ip|" . time(); //updating
    }
    }
    $x++;
    }
    if($alreadyIn == FALSE) {
    $users[] = "$ip|" . time();
    }
    //writing
    $fp = fopen($dataFile, "w+");
    flock($fp, LOCK_EX);
    $i = 0;
    foreach($users as $single) {
    if($single != "") {
    fwrite($fp, $single . "\r\n");
    $i++;
    }
    }
    flock($fp, LOCK_UN);
    fclose($fp);
    if($uo_keepquiet != TRUE) {
    echo '<b>' . $i . '</b>';
    }
    ?>

  • Login to your cPanel, click on File Manager and locate the root directory of the site you wish to display this, then upload the online.php¬†script you saved earlier. Ex: If you wish to display it on your root domain of your hosting account, upload it to public_html, if the domain is an Addon Domain, go to public_html/yourdomain.com and upload it there.
  • Now locate the part of your PHP script you wish to display the number of onliners and place this code

    <?php include('online.php'); ?>

    .

  • Save your script and View the Site.

How To Display Online Users in WordPress Without Plugins

  • Just like above, copy the first code and save as online.php, then upload to your domain root directory in File Manager of your cPanel (same as above).
  • Login to your WordPress Blog Dashboard, click on Appearance > Editor.
  • Locate the part of your theme you wish to display the online users (parts such as footer.php, header.php etc) click on it by the right sidebar of your editor.
  • Now copy and paste this code where you would have it displayed.

    <?php include('online.php'); ?>

  • Click on Update File to Save.
  • You can now view your blog to see the effect on your blog.

You can style the output text using CSS if you wish, it’s all up to you.

Note: The line in the PHP online.php code that reads $sessionTime = 1; denotes the time threshold to display the number of online users in minutes. For example 1 is used, this would display the number of onliners in the past 1 minute.

I hope this post was helpful.

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here