Chris Hope's LAMP Blog - The Electric Toolbox
Linux Apache MySQL and PHP articles by Chris Hope
This is Chris Hope's blog for Linux, Apache, MySQL and PHP, otherwise known as LAMP. "The Electric Toolbox" is the name of my company and I started this website several years ago with articles about web programming etc. I didn't update anything for some time but have recently begun posting a new article per day, which was always my intention. I have finally added an RSS feed and will shortly add the ability for users to post comments about the blog posts.
The ten most recent articles can be found below in their entirety. Navigating the sections in the right hand navigation (under Categories) will bring up all the other posts, and you can also use the search box at the top of the page to find what you might be looking for.
Weekly Roundup - May 12th 2008
Posted May 12th, 2008 in Weekly Roundup
This is my weekly roundup for the week of May 5th to 11th 2008, where I look back at the posts I made over the past week as well as useful and interesting articles on other websites and blogs that I might have read.
Articles posted on my blog
Monday: Weekly Roundup - May 5th 2008
Tuesday: Centering a page with HTML and CSS
Wednesday: Enable the administrator account in Windows Vista
Thursday: Access denied when logging into VMWare Infrastructure Web Access on Windows
Friday: Find the length of the longest string in MySQL
Saturday: Change Adobe Photoshop Filename Compatibility Options
Sunday: Database error handling with the Zend Framework
Interesting articles found offsite
Apache
David Walsh posted how to force SSL pages on Apache with .htaccess by using a rewrite rule to redirect if the server port is not port 80.
jQuery
query7.com posted how to manipulate HTML with jQuery.
KDE
KDE 4.0.4 was released. KDE 4.1 is due for release in July.
Linux
The Ubuntu Geek posted how to enable automatic login with Ubuntu. This means Ubuntu will automatically log in to Gnome as the specified user without having to enter a login name and password when your computer starts up.
Open Source News posted some useful BASH tips including filename completion, command completion, brace expansion and return codes.
Rod Smith at IBM looked at the ext4 file system, comparing it with ext3, migrating from ext3 to ext4 and maintaining an ext4 filesystem.
Geoff Palmer at PC World NZ looked at how to add the Ubuntu desktops (Kubuntu, Xbuntu etc) to your Ubuntu etc install. More details here.
Arc Technica posted a review of Ubuntu 8.04 Hardy Heron and Linux.com looked at whether you should do an upgrade or fresh install to get from previous versions of Ubuntu to the current version.
WatchingTheNet.com posted how to disable SSH logins for the root account.
.NET
PrimaryObkects.com looked at using the Model View Controller pattern in .NET applications.
OpenOffice.org
A beta version of OpenOffice.org 3.0 was released. Version 3 will now run on Intel Macs natively without the need for an X11 environment. More info at Ars Technica and PC World. The final release is currently scheduled for September.
PHP
PortEightyEight.com posted 9 PHP debugging techniques you should be using, including to enable notices, use a logging system, use an IDE and debugger and unit testing.
Nathan Good at IBM posted about what's coming up in PHP 6, including new features and things that are being removed.
Programming (General)
CodingHorror.com posted a useful explanation of what the Model-View-Controller is all about.
Windows
Windows XP Service Pack 3 was released. Microsoft have warned users that they won't be able to downgrade from Internet Explorer 7 after installing the service pack, should they wish to (more details here at PC World NZ and here at PC World US).
And in other Windows XP Service Pack 3 news... it's been reported that some PCs get stuck in endless reboots.
Other Stuff
Smashing Magazine posted reviews of 25 WYSIWYG editors a 35 source code / text editors, with screenshots and links through to each editor's website.
Database error handling with the Zend Framework
Posted May 11th, 2008 in PHP
When running database queries in the past, I have used my own database abstraction libraries, Code Igniter, the PEAR database library, ADODB and ADODB Lite. Each of these easily allows error handling by having a function called in the event of a database error occuring. I have recently started using the Zend Framework, but there doesn't appear to be this capability of automatic error handling. This short post looks at how to capture and handle database errors with the Zend Framework.
Basically, you need to put your database queries inside a try...catch block and in the catch block call your error handling function. This is easy enough but you have to remember to do it each time, whereas using the other database libraries there were hooks to call a user defined function in the event of an error so you didn't need to remember to invoke it each time.
So, with the Zend Framework you need to do something like this each time you call a database query:
try {
...
// code to run a database query etc here
...
}
catch Exception $e {
my_error_handler($e);
}
Then your error handling function could log the error to a file, display an error to the user and/or send you an email etc. You can get the message from the exception object with the ->getMessage() function.
Change Adobe Photoshop Filename Compatibility Options
Posted May 10th, 2008 in Applications
Adobe Photoshop's "Save for Web" function will truncate filenames to 31 characters by default to (apparantly) allow for compatibility with Mac OS 9 browsers. The dialog box which gives the warning about the filename truncation tells you to edit the output settings but it isn't that clear where to go to actually change the filename compatibility options. This post looks at how to do this. Note that if your filename is less than 31 characters the warning dialog shown below will not be displayed.
The error message is "Some names of the files being saved will be truncate to 31 characters for compatibility with Macintosh browsers. To change your filename compatibility options, click the Optimize pop-up menu, choose Edit Output Settings, and select Saving Files." This is shown in the screenshot below. You have the option to not show the message again by ticking the box, and can either accept the filename truncation by clicking the "OK" button or "Cancel" to return to the previous Save for Web screen.

It's not particularly obvious where the "Optimize pop-up menu" is in Photoshop's "Save for Web" dialog. I have highlighted the button with a red box in the screenshot below. On different versions of Windows and on a Mac the button may look slightly different, but it should always be in the same place. The screenshot below is from Windows Vista.

When you click the button, you will see a menu like the one in the screenshot below. Select the "Edit Output Settings".

You will now see the "Output Settings" dialog as shown in the screenshot below. Initially the drop down box I have highlighted with a green box will be for HTML settings; change it to "Saving Files" and your dialog window should look more or less like the one below.

The setting you need to change to allow the "Save for Web" function to allow filenames greater than 31 characters is the "Mac OS 9" checkbox in the "Filename Compatibility" section which I have highlighted with a red box in the above screenshot. Uncheck this box and then click the "OK" button.
You must then actually save your image for the setting to take effect; if you cancel out from saving the image the setting will be lost. However, once you make the change as shown above and then save the image, it will be remembered for the next time.
Find the length of the longest string in MySQL
Posted May 9th, 2008 in MySql
There are a number of string functions in MySQL for extracting text, working out the position of a substring, calulating the length of text and so on. This post looks at how to work out the length of the longest string in a field in a MySQL table.
The LENGTH() function in MySQL returns the length of a string in bytes. Multi-byte characters will count as multiple bytes. The examples in this post will use the LENGTH() function but you can substitute them with the CHAR_LENGTH() function instead if you want to count the number of characters rather than bytes. Note that CHAR_LENGTH will treat a character that uses two bytes a one single character and would return 1.
The first example below illustrates what LENGTH() will return:
SELECT LENGTH('this is a test');
This will return the number 14.
To work out the length of the largest string in a MySQL table, combine the LENGTH() and MAX() functions together like so:
SELECT MAX(LENGTH(field_to_query)) FROM table_to_query;
where "field_to_query" is the fieldname you want to query and table_to_query is the table. This will then return the length of the longest field named "field_to_query" as an integer.
This can be quite useful if you've loaded data from an external source into a table and want to ensure you have enough storage space for the data. I need to regularly load data from a text file into a MySQL table and one of the fields is labelled "long_description". Initially I created the field as a VARCHAR(255) and then ran the above query against the table only to discover the longest field(s) were 255 bytes long. I then changed the table to type TEXT and ran the query again and found out the longest field was acutally 1210 bytes long.
This final example will show all the unique lengths along with a count:
SELECT LENGTH(field_to_query), COUNT(*) FROM table_to_query GROUP BY LENGTH(field_to_query);
To order it by the those with the highest count first, add "ORDER BY COUNT(*) DESC" and to order it by the longest count first, add "ORDER BY LENGTH(field_to_query) DESC" e.g.:
SELECT LENGTH(field_to_query), COUNT(*) FROM table_to_query GROUP BY LENGTH(field_to_query) ORDER BY COUNT(*) DESC
SELECT LENGTH(long_description), COUNT(*) FROM table_to_query GROUP BY LENGTH(field_to_query) ORDER BY LENGTH(field_to_query) DESC
Example output for the first ten rows, ordered by the highest count first is as follows:
+------------------------+----------+ | LENGTH(field_to_query) | COUNT(*) | +------------------------+----------+ | 242 | 198 | | 644 | 170 | | 222 | 169 | | 180 | 151 | | 183 | 146 | | 176 | 146 | | 143 | 142 | | 201 | 142 | | 235 | 139 | | 218 | 138 | +------------------------+----------+ 10 rows in set (0.05 sec)
Access denied when logging into VMWare Infrastructure Web Access on Windows
Posted May 8th, 2008 in VMWare
A couple of days ago I decided to try out Beta 2 of the free VMWare Server version 2. After installing it on Windows Vista I attempted to log into the VMWare Infrastructure Web Access but got an error message saying "Access Denied". This post looks at how to successfully log into the web access console on Windows Vista.
When I first brought the web access login up in my web browser I was greeted with a login name and password dialog box. Not having had to specify these details when I installed VMWare Server I wasn't sure what to enter, so used my Windows Vista login name and password. I am an administrator on this particular machine so figured it may work.
Instead of logging me in I got the error message "Access Denied".
I knew from past experience when running VMWare Server 1 on Linux that to log into the console you needed to log in as a privileged user, and that Windows usually has a special "Administrator" account for this sort of stuff. A quick search on Google and this was confirmed from, so I just needed to enable the administrator account and assign it a password. My post from yesterday looks at how to enable the administrator account on Windows Vista, so refer to that for more details about how to do this.
After enabling the administrator account, I was then able to log into the VMWare Infrastructure Web Access console using the login "Administrator" and the new password I had set.
Enable the administrator account in Windows Vista
Posted May 7th, 2008 in Windows
Windows Vista comes with a built-in "Administrator" account which is disabled by default. It is possible to enable this account if for some reason you need to. This post looks at how to do this. Please be sure you really do need to enable this account before you follow these instructions as it is possible it may introduce other security issues.
First you need to get into the Computer Management Console and navigate to user management. To do this, from the "Start" menu (the round button on the taskbar with the Windows icon on it) and navigate like so:
Start -> Control Panel -> Classic View -> Administrative Tools -> Computer Management
User Account Control will then ask you for permission to access the computer management console. Click the "Continue" button and then navigate like so:
Local Users and Groups -> Users
Right-click the Administrator account and select the "Properties" option. The dialog that appears will look like the one in the screenshot below. Make sure the "Account is disabled" checkbox is unchecked and click the "OK" button to save your change and close the dialog.

Right-click the Administrator account and select the "Set Password" option. A warning dialog will be displayed as shown in the screenshot below. Click the "Proceed" button.

Now you can set the password using the change password dialog as shown in the screenshot below.

That's it - the Administrator account is now enabled (along with any security implications ;)
Centering a page with HTML and CSS
Posted May 6th, 2008 in HTML and CSS
This post looks at how to center the layout of a page in a web browser, so that it is initially centered and if the user resizes their browser it will remain centered. It is very easy to do using HTML and CSS with only a minor issue in older versions of Internet Explorer to deal with.
The basic skeleton would look this:
HTML:
<html>
<head>
...
</head>
<body>
<div id="container">
... other divs and content here ...
</div>
</body>
</html>
CSS:
#container {
width: 900px;
margin: auto;
}
Obviously you can set the width of the container to whatever you want it to be, 900px is just an example. All your other divs would then sit inside the container div.
This works in all versions of Firefox from 1.0 up (it may work in the earlier versions but that's the earliest I have on my browser test machine) and Opera 7.0 up (ditto Firefox comment). With Internet Explorer it will work with both Internet Explorer 6 and Internet Explorer 7 as long as a doctype has been specified. The above example doesn't include a doctype but as long as you include one then it will work in those versions of Internet Explorer.
In older versions of Internet Explorer, you need to add an additional declaration to the stylesheet. The modified version of the style sheet would then look like this, with the changes in bold:
body {
text-align: center;
}
#container {
width: 900px;
margin: auto;
text-align: left;
}
Adding text-align: center; to the style sheet makes everything centered in IE. However it will also make text in all other divs etc align to the center, so we fix this by adding text-align: left; to the container div. This will now allow the container to be centered for older versions of Internet Explorer (prior to IE 6.0) and also center the container in IE6 and IE7 if no doctype is specified.
Weekly Roundup - May 5th 2008
Posted May 5th, 2008 in Weekly Roundup
This is my weekly roundup for the week of April 28th to May 4th 2008, where I look back at the posts I made over the past week as well as useful and interesting articles on other websites and blogs that I might have read.
Articles posted on my blog
Monday: Weekly Roundup - April 28th 2008
Tuesday: How to tell if it's a leap year with PHP
Wednesday: Enable the Web Interface for uTorrent
Thursday: Monthly Roundup - April 2008
Friday: Reverse Mapping Checking - Possible Break-in Attempt Error with SSH
Saturday: Stop logwatch reporting on a particular service on CentOS
Sunday: logwatch "postdrop Illegal seek" and "sendmail queue file write error"
Interesting articles found offsite
BSD
Hardware
The downspec Toshiba Satellite A200 that I bought a few weeks ago was reviewed by NZ PC World.
jQuery
devkick.com posted links to a whole bunch of useful scripts/plugins/modules for jQuery.
KDE
KDE 4.1 Alpha 1 was released, and both PolishLinux and Ars Technica reviewed it, along with screenshots.
Linux
Slackware 12.1 was released. You can buy Slackware from the Linux CD Mall.
Microsoft/Yahoo
Microsoft's attempt to buy Yahoo has been big news over the last three months but Microsoft has finally given up. Read details at PC World, The Register and Ars Technica.
MySQL
The MySQL Performance Blog looked at the use of indexes on columns in a post titled "do you always need index on where column?", benchmarking a tinyint field with only two possible values on a table with 20 million rows.
Photography
Smashing Magazine posted 50 amazing photographs. Some of them have obviously been Photoshopped but there are some really great photos in there. I've seen a few of them before but most of them were new to me.
PHP
phpMyAdmin 2.11.6 was released. This is a bug fix only release.
Marco Tabini posted about 5 PHP 5 features you can't afford to ignore on his "Accidental Businessman" blog. These include SimpleXML, JSON and SOAP, PDO, SPL and SQLite.
builderau.com introduces SOAP with PHP using the nusoap library.
Windows
Windows XP Service Pack 3 was released, as reported by Ars Technica, and then held back due to a compatibility issue, as reported by The Register.
PC World posted 18 Features Windows Should Have (but doesn't). I'd really like to see the virtual workspaces built into Windows instead of having to use 3rd party add-ons which just don't work smoothly enough.
Other Stuff
PC World looked at how Xerox spun off their Palo Alto Research Center as a separate company and how it is now making a profit. PARC has come up with many modern computing innovations, such as the graphical user interface and ethernet. And Computer World looked at how Xerox/PARC has created erasable paper which can be reused up to 100 times.
logwatch "postdrop Illegal seek" and "sendmail queue file write error"
Posted May 4th, 2008 in Email Servers and Linux/Unix/BSD
logwatch is a daily process for reporting and analyzing log files and I recently started getting errors on one of the CentOS Linux servers I manage and no daily report. The particular machine gets thousands of bounced emails per day and the mail log files get very large. logwatch had been reporting to me every single bounced email message so the report emails had got very large before stopping altogether. This post looks at the error messages I got and what I did to fix the problem.
As I mentioned, the daily emails got really big and then stopped coming altogether, seemingly at random. The particular server sends a lot of email marketing messages every couple of weeks and gets a large number of bounced emails when they are sent. The error message I got in the email from logwatch was as follows:
/etc/cron.daily/0logwatch: postdrop: warning: uid=0: Illegal seek sendmail: fatal: root(0): queue file write error
After a little research I discovered that the default maximum size of an individual email on the version of Postfix running this particular server is 10 MB. Because logwatch was reporting on all bounced emails the email report being sent was sometimes larger than then 10 MB maximum, so I got the above error message instead.
There were two solutions to my problem: either switch off reporting of the mail service, or increase the size limit of the maximum email message.
To increase the maximum size of an email message in postifx, add or edit the "message_size_limit = XYZ" setting to the /etc/postfix/main.cf configuration file and then reload postfix.
I've never been particularly interested in getting large logwatch emails containing information about all the emails that bounced, so decided to switch off reporting about the mail services instead. Refer to my post from yesterday titled "Stop logwatch reporting on a particular service on CentOS" about how to do this.
Stop logwatch reporting on a particular service on CentOS
Posted May 3rd, 2008 in Linux/Unix/BSD
logwatch is a system log analyzer and reporter which emails daily reports about information in the system log files in /log. On CentOS and Red Hat Enterprise Linux this is usually configured by default to email the root user on a daily basis with the results from the log analysis. This post looks at how to stop logwatch from reporting on a particular service, for example sendmail and postfix.
The configuration files for logwatch on CentOS are stored at /etc/logwatch. The subdirectories and configuration files at this location are empty to start with and override the default settings, so you can safely add your own settings to these files without causing issues to the defaults. Simply remove your changes from these files at a later stage and you will restore the default behaviour.
To prevent logwatch from reporting on a particular service, for example sendmail or postfix, open up the following configuration file:
/etc/logwatch/conf/logwatch.conf
and add the following to the end of it to pevent log analysis of sendmail:
Service = "-sendmail"
or postfix:
Service = "-postfix"
The next time logwatch is run it will no longer report on the services with a "-" entry. You can do a test run of logwatch at any time by running the following on a CentOS or RHEL machine:
/etc/cron.daily/0logwatch
You do not need to restart any services for your configuration changes to take effect; they will be used the next time logwatch is run.


