Considering how regularly new versions of Firefox now come along, that's quite some bug fix list in version 10! http://t.co/K3I2vLpW 1 week ago


15th
Oct 08

Finding the number of users online



I often wondered how sites detected the number of users online. After some digging I’ll admit to be no closer to the answer. The problem is that once a page is delivered to someone you have no idea what they’re doing unless, and if, they then do something – go to another page of yours, click a button, etc. They could have immediately left the site and gone elsewhere… you simply don’t get back the level of interaction that would tell you this.

So my solution was based on simple maths. Using Google Analytics and the like I work out how long an average user spends looking at a page on the site. Let’s say it’s 90 seconds.

I’m using PHP and MySQL for this example. So, create a new table named users_online and create two fields for it – the first named timestamp and the second ip_address.

With your database already open execute the following PHP, pre-setting the variable $online_seconds with the figure we discussed a couple of sentences ago (in my example, 90). This should be at the top of each page.

$timestamp = time();
$timeout = $timestamp-$online_seconds;
$insert=mysql_query("INSERT INTO users_online VALUES('$timestamp','$REMOTE_ADDR')");
$delete=mysql_query("DELETE FROM users_online WHERE timestamp<'$timeout'");
$result=mysql_query("SELECT DISTINCT ip_address FROM users_online");
$user_num=mysql_num_rows($result);

This will add the period of time (90 seconds) to the current timestamp and insert a row into the table with that as a “timeout”. We then delete any rows where this timeour has now passed. Finally, we read the number of distinct IP addresses – this is the number of users currently online and is returned as $user_num.

How does this work? Well, the idea is that after 90 seconds if the user hasn’t moved onto another page or refreshed the current one then they’re gone. If they do, a new row with their IP address is created and they’re counted once more as still being active. Quite simple really. Possibly not the most accurate but without using host-side JavaScript, for instance, then I’m not sure how you would.

If nothing else it’s handy to have an idea as to how many people are online before you upgrade your site and potentially break it ;)

Delicious Digg Facebook LinkedIn Read It Later reddit StumbleUpon Twitter SeparatorEmail Google Translate PDF Online Print Friendly



14th
Oct 08

Create new useful links for your site


Google have announced a new feature in their Webmaster Tools. In a nutshell it will display all pages that link to a missing page on your site – you have a chance to fix it or take advantage of it.

So, if a site was linking to an erroneously spelt page then you could create a redirect for this to the correct page – ensuring you still get the visitors to the appropriate page.

BMTG was okay (apart from a misspelling on my own blog, so I was able to correct that) as was Copy+. However, I can see the advantage of this tool and will be keeping an eye on it in future.

Delicious Digg Facebook LinkedIn Read It Later reddit StumbleUpon Twitter SeparatorEmail Google Translate PDF Online Print Friendly



7th
Oct 08

PHP image thumbnails


I mentioned in a post yesterday that I also use PHP to generate image thumbnails. The idea is that any new photos I simply upload to the BMTG site, with a Picasa caption attached, and the site should do the rest.

Thumbnails are generated on-the-fly but only once as, once created, they are saved and re-used.

if (!file_exists($thumb_filename)) {
   $src_img=imagecreatefromjpeg($full_filename);
   $old_x=imageSX($src_img);
   $old_y=imageSY($src_img);
   $new_w=round(($old_x/$old_y)*100);
   $new_h=100;
   $dst_img=ImageCreateTrueColor($new_w,$new_h);
   imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$old_x,$old_y);
   imagejpeg($dst_img,$thumb_filename);
   imagedestroy($dst_img);
   imagedestroy($src_img);
}

$thumb_filename should contain the filename of the thumbnail and $full_filename is the filename of the original image (that the thumbnail is taken from). In the case of this example I’m creating thumbnails that have a height of 100 pixels and maintain their ratio, hence the width is variable.

The above can be made a lot more compact, but I’ve expanded it out to make it easier to follow.

By the time the code has finished, if the thumbnail didn’t already exist, it now does. Make sure the folder that the thumbnails are stored in has the appropriate permissions though!

Delicious Digg Facebook LinkedIn Read It Later reddit StumbleUpon Twitter SeparatorEmail Google Translate PDF Online Print Friendly