Non-local Online Users Script -- Evaluate my Code

Brackson

卍卍卍卍卍卍卍卍卍卍卍
Jun 20, 2013
262
46
Hello DevBest.

This is probably one of my first PHP scripts that I actually tried to make and understand, and I was wondering what you guys thought of it and if it could be optimized or not.

What this script does is reads the online count of an index page of a specific retro. I understand that not all retros have their online count in the same div, and I know that this script has to be personalized for every website that you use it on.

Also, please excuse the thread title; it's kind of hard to think of a name for something like this.

Here it is!

Save this as YOUR_FILE.PHP.
PHP:
<?php
    require_once 'simple_html_dom.php';
    // Download here http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download
    // It parses the HTML so we can get the div from the source easier than regular expression.
 
    /**
    * Gets number of online users
    * @return int The number of users online
    **/
 
    function getOnlineUsers() {
        $url = "http://xenon.pw/?novote";
        $html = file_get_html($url);
        $onlineUsers = $html->find('div[id="online"]');
        return intval(strip_tags($onlineUsers[0]));
}

and save this file something else.

PHP:
<?php
    require_once 'YOUR_FILE.php';
?>
 
<html>
<head>
    <title>Better script (by Heaplink)</title
</head>
 
<body>
    <p>There are <b><?=getOnlineUsers() ?></b> users online, irght now!</p>
</body>
</html>
*Special thanks to Heaplink for these two scripts, which are much more efficient than my first. Compare .

I used Xenon because I'm a staff member on it, and it has it's online count on it's index page (which makes it a lot easier). I figured that this script would be good for fansites and such, since it echo's the code as plain text, and you can style it how ever you want to.

What do you guys think?

This has not been released yet. Please do not release this.
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
Ok so.... Never, ever in your whole life from now on use regular expression to parse HTML. It's not only slow and inefficient, but you kill a baby seal every time you do it.
 

Brackson

卍卍卍卍卍卍卍卍卍卍卍
Jun 20, 2013
262
46
PHP:
<html>
<?php
include 'simple_html_dom.php';
// Download here http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download // It parses the HTML so we can get the div from the source easier thank using regular expression.
 
$url = "http://xenon.pw/?novote";
// The URL of the retro that you want to use.
 
$html = file_get_html($url);
// Gets the HTML source of that link and stores it in a variable.
 
$ret = $html->find('div[id=online]');
// Finds the div where online user count is stored.
 
$onlineusers = (strip_tags($ret[0]));
// Converts the div into non-markup'ed text.
 
echo $onlineusers;
// Echos the online users!
 
?>
</html>

Updated code.

Thanks Heaplink for advising not to use regular expression. This way is a lot easier to use, and a lot easier on the eyes!
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
Next...

This is the errors:

- PHP inside HTML markup (unless you really wan't to echo HTML, but I recommend NOT to do it)
- The content does not go into HTML tags, it goes in body tags (google for a HTML5 template)
- Use require_once to make it faster, and more stable (include will include a file no matter what, require_once ensures this only happens once and outputs proper errors/warnings)

Yeah...

Also when you're not using PHP inside any HTML markup (like a PHP only file) you don't need the ?> (php end of file). This is also faster in the longer run.
 

Brackson

卍卍卍卍卍卍卍卍卍卍卍
Jun 20, 2013
262
46
Next...

This is the errors:

- PHP inside HTML markup (unless you really wan't to echo HTML, but I recommend NOT to do it)
- The content does not go into HTML tags, it goes in body tags (google for a HTML5 template)
- Use require_once to make it faster, and more stable (include will include a file no matter what, require_once ensures this only happens once and outputs proper errors/warnings)

Yeah...

Also when you're not using PHP inside any HTML markup (like a PHP only file) you don't need the ?> (php end of file). This is also faster in the longer run.
PHP:
<!DOCTYPE html>
<body>
<?php
require_once 'simple_html_dom.php';
// Download here http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download // It parses the HTML so we can get the div from the source easier thank using regular expression.
 
$url = "http://xenon.pw/?novote";
// The URL of the retro that you want to use.
 
$html = file_get_html($url);
// Gets the HTML source of that link and stores it in a variable.
 
$ret = $html->find('div[id=online]');
// Finds the div where online user count is stored.
 
$onlineusers = (strip_tags($ret[0]));
// Converts the div into non-markup'ed text.
 
$onlineusercount = filter_var($onlineusers, FILTER_SANITIZE_NUMBER_INT);
// Takes the number of users online in just number format and stores it in a variable.
 
echo $onlineusercount;
echo " users online!";
// This is what comes after the number of users.
?>
</body>
</html>

If someone wanted to use the PHP code without it being in the HTML markup, then they can. I'd rather not though. I fixed the body tags, although I only put <html> in there to show that it was an .html file. I took your advice and used require_once, as it's more efficient.

I also added where you take only the numbers of the users online and then you can add something after it. (ex: "7 user(s) online", vs "7 users online!", or honestly pretty much "7" anything), becuase I realized that this could cause some conflict with the diversity of how retros display this type of information.
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
I fixed the body tags, although I only put <html> in there to show that it was an .html file.

A HTML file cannot be interpreted as PHP (unless you use mod_rewrite - but is a very unsecure and bad way to do). Do something like this instead:

PHP:
<?php
require_once 'simple_html_dom.php';
// Download here http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download // It parses the HTML so we can get the div from the source easier thank using regular expression.
 
/**
* Gets number of online users
* @return int The number of users online
**/
function getOnlineUsers() {
    $url = "http://xenon.pw/?novote";
    $html = file_get_html($url);
    $onlineUsers = $html->find('div[id="online"]');
    return intval(strip_tags($ret[0]));
}

and in another file you can do this:

PHP:
<?php
    require_once 'YOUR_FILE.php';
?>
<html>
<head>
    <title>Better</title>
</head>
<body>
    <p>There are <b><?=getOnlineUsers()?></b> users online, right now!</p>
</body>
</html>

A rule of thumb in PHP is require/include and validate at the top of the page if you're going to have markup with your php. Then mostly to condition checking and echoing in the markup as above <?=?> is the same as <? echo ?>

Note: <? and <?= are short tags. These has to be enabled in php.ini in order for them to work (by default in PHP >=5.1 it is enabled - might also be in older versions of PHP.)
 

Brackson

卍卍卍卍卍卍卍卍卍卍卍
Jun 20, 2013
262
46
A HTML file cannot be interpreted as PHP (unless you use mod_rewrite - but is a very unsecure and bad way to do). Do something like this instead:

PHP:
<?php
require_once 'simple_html_dom.php';
// Download here http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download // It parses the HTML so we can get the div from the source easier thank using regular expression.
 
/**
* Gets number of online users
* @return int The number of users online
**/
function getOnlineUsers() {
    $url = "http://xenon.pw/?novote";
    $html = file_get_html($url);
    $onlineUsers = $html->find('div[id="online"]');
    return intval(strip_tags($ret[0]));
}

and in another file you can do this:

PHP:
<?php
    require_once 'YOUR_FILE.php';
?>
<html>
<head>
    <title>Better</title>
</head>
<body>
    <p>There are <b><?=getOnlineUsers()?></b> users online, right now!</p>
</body>
</html>[/CODE]
 
A rule of thumb in PHP is require/include and validate at the top of the page if you're going to have markup with your php. Then mostly to condition checking and echoing in the markup as above <?=?> is the same as <? echo ?>
 
Note: <? and <?= are short tags. These has to be enabled in php.ini in order for them to work (by default in PHP >=5.1 it is enabled - might also be in older versions of PHP.)
Alright, thanks so much for sharing this. I'm still learning, but I think that I just learned a lot more through this script ;).

OP has been updated to include these scripts.
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
Even small things can help you further in learning new stuff, that's how I did it. Take the steps small and grow as you feel for more challenge.
 

Users who are viewing this thread

Top