Tag: php

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.

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

WikiVS: MySQL vs. PostgreSQL. Why Would You?

I just came across this article published last week from the WikiVS guys. As a web developer I normally use MySQL, however I have been working on some projects recently that MySQL just hasn’t been able to handle because of the sheer number of rows used. The most recent is a program that reads apache log files and allows real time awstats style reporting, as well as comparisons between all websites that are being tracked. As you can imagine, when tracking stats for over a hundred websites, many of which get more than several thousand unique hits a day, the number of rows quickly flew into the millions and that combined with a highly relational structure brought MySQL crying to its knees. Even on an 8 core machine with as many parallel programming techniques that PHP5 would let me lay my hands on!

In jumped PostgreSQL to the rescue, along with my own query caching method and the software was back up and running!

Quote

MySQL vs PostgreSQL is a decision many must make when approaching open-source relational databases management systems. Both are time-proven solutions that compete strongly with proprietary database software. MySQL has long been assumed to be the faster but less full-featured of the two database systems, while PostgreSQL was assumed to be a more densely featured database system often described as an open-source version of Oracle. MySQL has been popular among various software projects because of its speed and ease of use, while PostgreSQL has had a close following from developers who come from an Oracle or SQL Server background.

Get your PHP on the right Trax

Original Article on The Register

Hands on Ruby on Rails has become a popular framework for developing database-based web applications using the Model-View-Controller (MVC) pattern.

Before Ruby on Rails, though, PHP was hogging the web-development limelight. Problem was, there was no Model-View-Controller (MVC) framework for PHP.

With Ruby on Rails, though, PHP developers have come to realize the timesaving benefits of MVC – a fact that led to the development of various PHP frameworks that are actually based on Ruby on Rails.

Among them, the Akelos framework and PHP On Trax.

In this article we shall develop an MVC Create-Read-Update-Delete (CRUD) application using the PHP On Trax Framework. Why this particular framework? Simple: it is a direct port of the Ruby on Rails framework.

I haven’t managed to try this out yet but it I didn’t see anything in it that would’t be able to be set up in a linux environment, The article goes into a fair amount of detail (3 pages) in how to get it working. If anyone has had a go at this let me know how you have got on, I don’t really have the time at the moment!

Good Bye PHP 4

Link Here

Good bye and thanks for all the fish…