Advertisement:

Modification to show number of new posts in browser title bar

Aloittaja cisops, elokuu 06, 2014, 01:02:23 IP

« edellinen - seuraava »

cisops

Hello all.

I'll preface my post by saying that I'm not a PHP programmer. I just aped some PHP code examples to get this done. If anyone would like to use this code, feel free to do so. Please share any feedback on ways to improve it, or someone can use it to create an actual SMF mod.

This code is meant to simple show the number of new posts in the title bar of the browser by updating the browser's page title using Javascript. That way, you don't have to constantly refresh to see if there are new posts.  ;)

Prerequisite is that you include the jQuery library. I just link to it on Google Code.

Here's the javascript you'll need on the page or preferably in a linked .js file. This code just uses jQuery's AJAX call to get the number of new posts in publicly accessible boards.


// for new post notifications in browser title bar, 2014-08-06
$(document).ready(function($) {

    var lastpost = 0;
    var newposts = 0;
    var title = document.title;

    // flash title every second when new posts are available
    // setInterval(function(){
    // if (newposts != 0)
    // { document.title = (document.title == title ? '(' + newposts + ') ' + title : title) }
    // }, 2000);

    // poll server for new posts periodically
    setInterval(function() {

        $.ajax({
            type: "GET",
            url: "/anynewposts.php",
            data: {
                last: lastpost
            },
            dataType: "html",
            success: function(data) {
                if (lastpost == 0) {
                    lastpost = data;
                } else {
                    if (data != 0) {
                        document.title = '(' + data + ') ' + title;
                        newposts = data;
                    }
                };
            }
        })
    }, 30000);
});


And, here's the PHP code which will need to be in a file called anynewposts.php.


// db credentials
$db_server = 'localhost';
$db_name   = 'yourdbname';
$db_user   = 'yourdbusername';
$db_passwd = 'yourdbpassword';

// get query string parameter indicating the last post viewed
$last_post_seen = $_GET['last'];

// make sure it's a number to prevent sql injections / xss
if (!is_numeric($last_post_seen)) {
    echo 'Bad value!';
    exit;
}

// connect to server
if (!$link = mysql_connect($db_server, $db_user, $db_passwd)) {
    echo 'Could not connect to server.';
    exit;
}

// open db connection
if (!mysql_select_db($db_name, $link)) {
    echo 'Could not connect to database.';
    exit;
}

// if this is the first poll (lastpost=0) get the current top message ID, otherwise get the count of new messages
if ($last_post_seen != 0) {
    $sql = 'SELECT COUNT(*) as newcount
FROM  smf_messages
WHERE id_msg > ' . $last_post_seen . '
AND id_board
IN (
SELECT id_board
FROM  smf_boards
WHERE  member_groups LIKE  \'%-1%\'
)';
} else {
    $sql = 'SELECT max(id_msg) as newcount
FROM  smf_messages
WHERE id_msg > ' . $last_post_seen . '
AND id_board
IN (
SELECT id_board
FROM  smf_boards
WHERE  member_groups LIKE  \'%-1%\'
)';
}
;

// get resulting query value
$result = mysql_query($sql, $link);

// was there an error?
if (!$result) {
    //echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

// get the result and echo it
while ($row = mysql_fetch_assoc($result)) {
    echo $row['newcount'];
}

// close db connection
mysql_free_result($result);



For some reason, I can't get it to work in IE unless I'm in debug mode, which is odd. If someone can figure out why that's the case, I'd appreciate some help.

Example of what it looks like in the browser tab attached. In the example, there are 4 new posts.

Enjoy.

Shane

Arantor

Wouldn't rely on is_numeric. The list of things that are 'numeric' is much larger than you might want to speculate. I'd personally be type-coercing to (int) and checking > 0.

Really not a fan of queries not passed through a proper query layer even if they are (mostly) sanitised first.

Also I'd note that you're limiting it to boards with certain visibility criteria, which may or may not be the boards visible to the current user, and it even gets applied to guests which seems like a non-trivial additional load being added to the site for lurkers.
Holder of controversial views, all of which my own.


cisops

Thank you for the critique. The point was that I'm putting this out to the public domain to maybe inspire someone else to make it better and more accessible to others.

So, rather than criticize, how about a stab at improving it and posting some improvements back to the thread. Like I said, I'm not a PHP programmer. I mostly code on the dark side, .NET.

Arantor

*shrug* You have designed it how you think it should work, and I think there's fundamental problems with the design even before we get to the implementation aspect, so to be honest I'm wary of even suggesting any more than fairly general critique.

If nothing else, every single user polling the server every 2 seconds is not a good idea, especially if users ever plan on opening multiple tabs (which *all* have their own polling going on) and that's even before we tackle the headache of making it for all the boards a user can see which spikes up the effort by at least a factor of 4.

I'm sorry I won't give you the improvements you were hoping for.
Holder of controversial views, all of which my own.


cisops

It's polling every 30 seconds, and the code can be modified, not every 2 seconds. If you're going to criticize, at least take a moment to read what you're criticizing.

I run a forum with 80,000 unique visitors a month with 900,000 page views a month. So, I can tell you that it hasn't caused any performance issues with a forum of that size.

I'd appreciate no more of your "help".

Anyone out there who wants to actually collaborate?


cisops

Update: Added the "cache:false" parameter to the AJAX call to fix the problem with IE.

Burke ♞ Knight

With how rude you are, towards an experienced PHP coder, and telling him off when he gives criticism on the coding, that you even admit is not that good of coding, what do you expect????

You can not come here, and ask for some help in the coding, then go and get an attitude when the people do what you ask, and try to help. Help comes in ALL forms, one of which, is CRITICISM!

If you can not understand that, then don't bother asking for help, ideas, advice, if not willing to hear the good and the BAD replies.

Advertisement: