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


21st
Nov 08

Reading RSS with PHP



After using the JavaScript only and generally pretty poor facility within Feedburner to embed RSS feeds onto a website, I set about an alternative solutions.

Thankfully, there are a number available. However, many offer little embedding options and I wanted something that gave me full control. This narrowed it down to 2 free downloadable scripts. However, as much as I tried I couldn’t get them to work properly.

Everything went well until it came to a link in my item description – then the less than/greater than signs would mysteriously disappear. Bizarrely, if I used the option to run it from the authors script from their site, it worked. So I can only assume it’s a configuration on my server. However, I’ve not been able to work out what it is.

Now, PHP has built in XML parsing options and this is what these scripts were using – and I’m guessing the configuration option in question was to do with that. So I gave up and decided to write my own alternative. It doesn’t use XML parsing but simply reads the whole file in and extracts the details I need – in this case title, description and date. The code is below – pass the RSS feed into it by assigning it to the variable $rss_feed.

$array=file_get_contents($rss_feed);
$i=0;
while ($i!=1) {
  $item_start=strpos($array,"<item>");
  if ($item_start===false) {
      $i=1;
   } else {
      $item_end=strpos($array,"</item>");
      $item_length=$item_end+7-$item_start;
    $item_strip=substr($array,$item_start,$item_length);
      $title_start=strpos($item_strip,"<title>");
    $title_end=strpos($item_strip,"</title>");
     $title_length=$title_end+8-$title_start;
    $title=substr($item_strip, $title_start+7, $title_length-15);
      $pubdate_start=strpos($item_strip,"<pubDate>");
      $pubdate_end=strpos($item_strip,"</pubDate>");
    $pubdate_length=$pubdate_end+10-$pubdate_start;
      $pubdate=date("jS F Y",strtotime(substr($item_strip,
$pubdate_start+9, $pubdate_length-19)));
      $description_start=strpos($item_strip,"<description>");
    $description_end=strpos($item_strip,"</description>");
     $description_length=$description_end+14-$description_start;
      $description=substr($item_strip, $description_start+13,
$description_length-27);
    $array=substr($array,$item_end+7);
 }
}

I’ve left some of the code expanded – for example there is no need for each field to be calculating and storing the end position as this could simply be placed within the length calculation. However, so you can see what I’m doing I’ve left it as it is.

So, during each iteration you will have 3 fields available for you to use, and probably display. They are $title, $pubdate and $description. $pubdate is converted into a more readable format using the date command, but the parameters can be easily changed to format it in another way.

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



4th
Nov 08

Reading Photoshop captions in PHP


Last month I posted about how to read Picasa captions in PHP.

Now that I own Photoshop Elements I decided I’d check to see where that applications stores its captions. And after some testing I can reveal… just the same place as Picasa.

Kind of. Well, if you give a photo a caption using Photoshop Elements then my previous script will read it without any issue. However, if you add the caption using Picasa then view the photo in Photoshop Elements… it doesn’t recognise it. The other way around, however, works. Weird.

But, hey, it means you can use my same script to recognise the caption no matter which application you use.

I’m assuming Photoshop works in the same was as Photoshop Elements – if anybody tries it, please let me know the outcome.

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



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