IT, Tech & Internet

Pesky Directory Alphabet Filters Made Easy (PHP)

I have built quite a few directory systems in my years as a web developer and one thing that has always annoyed me is building letter filters to return results depending on the first letter of a title.

Normally I would waste a lot of time actually typing out the entire alphabet string:

$alphabet = 'abcdefghijklmnopqrstuvwxyz';

which can take a lot of time (Go on try it, I’ll bet no matter how fast you can type, typing out the alphabet quickly will be tricky!) then looping through the string like so:

for($i = 0; $i < strlen($alphabet); $i++)
{
  echo substr($alphabet, $i, 1);
} 

However there are 2 other much simpler methods:

Method 1 – Loop through the ASCII numbers

for($i = 65; $i <= 90; $i++)
{
  echo chr($i);
}

or my new personal favourite using PHP’s range() function:

Method 2 – Using range()

foreach(range('A', 'Z') as $i)
{
  echo $i;
}

Then all you have to do is add in any code to make sure you set the right CSS selectors and highlight which letter you are currently viewing and add the ability to filter by first letter to your list function.

Simple.

The RSS Cloud – Real Time Blogging

As with any WordPress Blog and pretty much any other blog or news website projectroon.co.uk has an RSS Feed and if you look closely at the source of this RSS feed you may notice a new element

<cloud>

What is this? Well currently most Feed Aggregators will poll selected RSS feeds and check if there are any new items. This can be slow and resource heavy especially for someone like Google who has to check millions of feeds many times every day with their reader technology

The cloud element has always been available in RSS 2.0 however most have ignored it or failed to appreciate its possible uses.

Anyone who is familiar with a Blackberry mobile phone will know just how much money they save in data charges by using the Blackberry’s Push technology. Push technology means that the phone doesn’t have to keep connecting to it’s mail server and asking if there are any new emails available. Instead the server waits until it has a new email and then contacts the phone to deliver it. This results in far less load on the mail server and means you don’t have to wait to receive your email.

The RSS cloud works in the same way, feed readers that support the cloud element will be informed every time a blog owner publishes a new post, meaning readers using that reader will be able to start reading instantly after posting.

Find out more from the Official Announcement by WordPress or add support for the RSS cloud for your wordpress blog here

Advertising is damaging the Internet experience

In my humble little opinion the original intention of the web was to be a worldwide free medium to share documents and discuss idea. So far this ideal has been somewhat achieved but there is something that is threatening the ideal.

I have recently stopped following some blogs and websites, not because I find their content irrelevant or or lackluster, but because my experience whilst on these sites has become so tarnished by advertising that I can’t read the content. Some of these sites are industry leaders in breaking news for the technology and IT worlds, but if I cannot read the content I am not going to bother coming back.

Now I am not saying that advertising is all bad, most of these blogs and websites depend on it to remain publishing. However as the average user learns to automatically and involuntarily tune out the distractions, advertisers keep coming up with new innovative methods of annoyance.

I am at breaking point.

First we had JavaScript pop ups loading advertising pages up continually in your face and these were easily stopped by blocking JavaScript. These were soon replaced by flash banners normally at the top or sides of a website, which flash, scream or try to get you to shoot a duck.

Oh yes the ones that really drive me mad are the holding page adverts, where if you don’t have a cookie for a particular site you have to sit and wait for 10 seconds while several adverts are paraded in front of your face.

Now we have the CSS/Ajax pop up windows asking stupid questions like

“As a valued visitor please take a moment to tell us what you think about our site, and then if you wouldn’t mind visiting one or two of our sponsors…”

So I am quite certain that the vast majority of Internet users will agree with me here, but by charging less and less for more intrusive adverts, the only long term success that will be gained will be in driving readers away, not wealth.

The web is now one of the most powerful mediums on the planet, surely there is a better way of generating revenue whilst still maintaining a meaning?

How to get Ubuntu 8.04 Server running Lighttpd ready for the mighty WordPress

Lighttpd - Fly Light

Lighttpd - Fly Light

This site has always been hosted on shared hosting, along with a few other blogs I maintain for friends (On The Wrong Planet and Darth McCarth to name a couple…). Even while I was freelancing I have always used shared hosting as it has always been perfect for my needs However the popularity of this blog has grown a lot in the last 7 months so I felt it was time to invest in some meatier hosting to something that would give me more control over the resources available to me and my site.

I don’t think that I require a dedicated server quite yet, especially not considering how much they cost for a reasonable one, so I decided to take the Virtual Server route. There are many very decent offers around, I chose to go with CheapVPS (A brand of VAServe Ltd) on the reccomendation of a colleague and Linux Engineer. Right lets get started then!

Install Lighttpd (Lighty)

I am assuming that you already have Ubuntu 8.04 installed on your VPS here as most VPSs come with the operating system pre-installed or provide a system like HyperVM to manage your server setup. I am also assuming that you have root access to your server, which is also provided by most VPS. If you don’t have root access then you will need to prepend these commands with sudo.

Log in as root and type:

apt-get install lighttpd

Watch as Lighty is downloaded and installed on your system. Once Lighty is installed you may want to remove Apache as it will probably just get in your way! To remove type:

apt-get remove apache2

If you have installed with a default setup then you will find Lighty’s main configuration at /etc/lighttpd/lighttpd.conf and the service script will be located /etc/init.d/lighttpd

to start, stop, restart or reload Lighty use these commands respectively:

/etc/init.d/lighttpd start
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd restart
/etc/init.d/lighttpd force-reload

Configuring Lighty for Virtual Hosting

In a similar fashion to Apache, Lighttpd keeps its configuration files in one location and expects you the create a symlink to them from another location to active certain features. These two folders can be found at /etc/lighttpd/conf-available and /etc/lighttpd/conf-enabled.

To enable virtual hosts and create the symlink type:

ln -s /etc/lighttpd/conf-available/10-simple-vhost.conf /etc/lighttpd/conf-enabled/10-simple-vhost.conf

Before creating the Lighty configuration for the virtual hosts we need to create all of the folders and files that are going to be required. As I allow my friends access to the server to modify their own websites I need to make sure that they don’t have access to other users websites. To create a user type:

adduser username

Where username should be replace by whatever username you want. Once a user is created we need to create the folders where their website files and logs are going to be stored and give them the correct permissions and ownerships. Again replace username with whatever you like and domain.com with your domain:

mkdir /home/username/domains
mkdir /home/username/domains/domain.com
mkdir /home/username/domains/domain.com/html
mkdir /home/username/domains/domain.com/logs
touch /home/username/domains/domain.com/logs/access.log
touch /home/username/domains/domain.com/logs/error.log
chown -R username:username /home/username/domains
chown www-data:www-data /home/username/domains/domain.com/logs/*

You can repeat this process for however many users and domains that you require. Or if you know what you are doing you could create a script to automate this process.

Now lets edit the virtual hosts config file:

vi /etc/lighttpd/conf-available/10-simple-vhost.conf

Now here is an example config block for pureroon.co.uk:

$HTTP["host"] =~ "(^|.)(panicroon|pureroon|roon)(.co|.me).uk$" {
    server.document-root = "/home/roon/domains/pureroon.co.uk/public_html"
    server.errorlog = "/home/roon/domains/pureroon.co.uk/logs/error.log"
    accesslog.filename = "/home/roon/domains/pureroon.co.uk/logs/access.log"
    url.rewrite = (
        "^/(wp-.+).*/?" => "$0",
        "^/(sitemap.xml)" => "$0",
        "^/(sitemap.xml.gz)" => "$0",
        "^/(robots.txt)" => "$0",
        "^/(xmlrpc.php)" => "$0",
        "^/(favicon.ico)" => "$0",
        "^/(cgi-.+).*/?" => "$0",
        "^/(icon.+).*/?" => "$0",
        "^/(.+)/?$" => "/index.php/$1"
    )
}

The first line

$HTTP["host"] =~ "(^|.)(panicroon|pureroon|roon)(.co|.me).uk$"

is telling lighty which domain to look out for, you can use regular expressions here and you can see that I am capturing panicroon.co.uk, pureroon.co.uk and roon.me.co.uk. I then let wordpress redirect the user to the correct domain (pureroon.co.uk).

The next 3 lines are telling lighty where to find the document root (the location of your web files), and the locations of the error log and access log respectively.

To be able to use URL rewriting in your wordpress blog you need to set up the url.rewrite lighty module. The problem I have found with the url rewriting is that lighty doesn’t support the Apache ‘if file doesn’t exist function’ so you have to give it a list of files that shouldn’t be rewritten:

"^/(sitemap.xml)" => "$0"

will redirect ‘/sitemap.xml’ to itself. The final line:
"^/(.+)/?$" => "/index.php/$1"
redirects all other requests to ‘/index.php’ and the wordpress software will take care of everything from there.

That’s it, restart Lighty and you are ready to start installing MySQL and finally WordPress, check back soon for the next two stages