Close Menu
My SiteMy Site
  • Home
  • Reviews
  • Computers
  • How Tos
  • Travel & Lifestyle
  • Phone ROMs
  • Contact
    • Advertise
    • About Us
Facebook X (Twitter) Instagram
My SiteMy Site
  • Home
  • Reviews
  • Computers
  • How Tos
  • Travel & Lifestyle
  • Phone ROMs
  • Contact
    • Advertise
    • About Us
My SiteMy Site
Home»Programming»PHP»How to Display Online Users in WordPress and PHP Scripts
PHP

How to Display Online Users in WordPress and PHP Scripts

Oscar FrankBy Oscar Frank3 Mins Read
Share
Facebook Twitter LinkedIn WhatsApp Pinterest Email

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.

php programming

Related Posts

Common Mistakes Newbie Developers Make and Solutions

March 23, 2021

7 Best Laptops For Coders In 2025

February 28, 2020

10 Most Popular Languages to Learn Programming in 2025

April 4, 2019
View 3 Comments

3 Comments

  1. Smart on February 3, 2013 8:12 pm

    Nice TUTORIAL… Even Though its Old

    Reply
    • Oscar Frank on February 3, 2013 8:24 pm

      Really, you knew it?

      Reply
  2. Obasi Miracle on July 16, 2013 12:28 pm

    Lovely tutorial, thanks for sharing all these

    Reply
Leave A Reply Cancel Reply

Recent Posts
  • 6 Sports Apps You Must Have On Your Phone
  • Valencia Travel Guide: All You Need to Know About This Spanish Gem
  • TikTok Faces Unexpected Ban in Senegal, Sparks Global Curiosity
  • The Apple GPT & Apple’s Chatbot Mysterious Debut
  • Triller Takes on TikTok: Filing for Public Listing
Get Exclusive Tech Insights!
My Site
Facebook X (Twitter) Instagram YouTube
© 2025 Oscarmini Co. Designed by illBytes.

Type above and press Enter to search. Press Esc to cancel.