Archive for May, 2008

Writing valid HTML or XHTML

Thursday, May 29th, 2008

More than 98% of the web pages out there do not pass the W3C validation. You can verify this by running your favorite site on the W3C validation page.  It’s very hard for web developers to keep track of every closing ‘/’ (slash) on single tags or ALT text on images, specially when you are in a project that has a dead line of Yesterday!, sounds familiar?

If you want to write perfect HTML or XHTML code, I recommend installing the HTML Validator Plug-in for Firefox.  The HTML Validator Plug-in places it self in on the status bar.

Html Validator

It gives you live validation for any web site you visit.  It will also give you tips on how to fix your code when double clicking on the icon.

After installing this plugin, you will be amazed (annoyed) to know how many web sites have broken HTML.

MySQL Hierarchical Data

Wednesday, May 21st, 2008

Two common ways of storing hierarchical data in MySQL are the Adjacency List Model and the Nested Set Model.  The Adjacency uses a `parent id` to determine de parent of a record. The Nested Set Model uses the `left` and `right` values to determine the parent.


Adjacency List Model

Pros

  • Easy to implement
  • Queries are simpler to write
  • Fast updates
  • Fast Inserts

Cons

  • Retrieving the data is slower since it requires recursion to do it.

Nested Set Model

Pros

  • Faster selects.
  • Indefinite number of levels since with one query.

Cons

  • Queries are a little bit more complicated to write.
  • Updates are slow since multiple rows have to be updated.
  • Inserts are slower since multiple rows have to be updated.


Sample table structure for Adjacency List Model:

CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);

Sample Table Structure for Nested Set Model:

CREATE TABLE nested_category (
 category_id INT AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(20) NOT NULL,
 lft INT NOT NULL,
 rgt INT NOT NULL
);

For more information on how to use these models visit the MySQL article by Mike Hillyer at http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

Forward mail to another MTA (overwriting the php mail() function)

Thursday, May 8th, 2008

This post explains how to forward all the mail for an existing site using an MTA other than the site’s server it self. The usual technique to achieve this is to configure the local MTA to forward all the mail to another server. In this document I apply a different technique that overwrites the mail() function call to the `sendmail` command.

The advantage of this technique is that no extra headers are added to the message envelope. Why it is necessary to avoid adding extra headers differs from business to business.

Note: the technique here only applies to web applications running on *Nix machines. Windows machines running PHP applications can provide host and port in the php.ini or the apache configuration to achieve the same results.

Overwriting the sendmail Call

There is a couple of ways the `sendmail` path can be overwritten. The most common one is the php.ini and the least common one the php_admin_value directive in the apache configuration. We are going focus on the later one since this directive gives us the possibility to change the value for a single site on a machine that runs multiple virtual web accounts.

Configuration:

1. AS root create a directory for your `sendmail` replacement
mkdir -p /usr/local/forwarder

2. Download the SMTP Class.
wget http://theclickpro.com/articles/wp-content/uploads/2008/05/smtpclassphp.txt -O /usr/local/forwarder/smtp.class.php

3. Download the sendmail replacement script
wget http://theclickpro.com/articles/wp-content/uploads/2008/05/mailphp.txt -O /usr/local/forwarder/mail.php

4. Open the mail.php file on your favorite editor and change the $HOST, $default_from and
$default_return_path variables to suit your needs.

$HOST = 'mail.example.com'; //mx server to forward to
$default_from = '"Web Master" <webmaster@example.com>'; //default from line
$default_return_path = '<mailer-daemon@example.com>'; // address to deliver the bounces to

5. Open your Apache virtual host configuration file for your domain and put the following line some where close to the <VirtualHost> or </VirtualHost> tags
php_admin_value sendmail_path /usr/local/bin/forwarder/mail.php

6. Restart you Apache server.

Enjoy