March 1, 2007

Aligning images in text

You will find other articles relevant to this document in these sections:
Richard Lee @ 11:49 am

If you need to nicely align and image in a line of text here’s how you do it

.inline-img {
 
/* Firefox */
vertical-align:middle;
display:-moz-inline-block;
display:-moz-inline-box;
/* Everybody else */
display:inline-block;
 
}

Presto!

February 22, 2007

Flash based alternative to mailto:

You will find other articles relevant to this document in these sections:
Richard Lee @ 5:11 pm

When publishing a number of email addresses to a web page you run the chance of a spam bot scraping your site. There are a number of alternatives; such as using a mail form, obfuscating the mailto link with JavaScript or even converting the address to an image. While mail forms will conceal your email address from spam bots, they also conceal your address from the users who may simply want your address for other correspondence. Obfusticating your mailto link with JavaScript is also pretty useless since, being client site any spam bot can “resolve” the link much like your browser. And last but not least using images to display your address takes away the linking functionality. So I suggest a different alternative - Why not use Flash to display the address?

Using Flash, we can load the address client side in parts; “username” “domain” and form the address in a Flash textfield. Then we can simply use the getURL() function OR better use the textfield htmlText with an anchor to simulate our mailto link.
Here’s a hardcoded example of how I do it:
1. Create a 200px x 18 px document (FPS doesnt matter)

2. On Layer 1 add a textfield to the first frame, setting it to type dynamic, and give it an instance name of address_txt
3. Create a new layer above the previous layer for your Actionscript and add the following:

address_txt.htmlText = (username != undefined && domain != undefined) ? '<a href="mailto:'+username + '@' + domain + '">'+username + '@' + domain+'': "no email";</a>

4. Now using Publish your document (making sure you publish a html page in addition to your swf file)

5. Open your published html file in Dreamweaver or your favourite editor and append the querystring “username=joebloggs&domain=gmail.com” to the

<param movie="..." ></param> and <embed src="..."> </embed> tags:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="200" height="18" id="mailto" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="mailto.swf?username=joebloggs&domain=gmail" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed xsrc="mailto.swf?username=joebloggs&domain=gmail.com" quality="high" bgcolor="#ffffff" width="200" height="18" name="mailto" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

6. Finally run your movie in the browser and you should see your email address in the Flash textfield!

December 1, 2006

Google prefering dashes to underscores

You will find other articles relevant to this document in these sections:
Cameron Manderson @ 7:49 pm

Bit of an interesting post on Google prefering dashes and underscores: http://www.mattcutts.com/blog/dashes-vs-underscores/

October 25, 2006

Creating readable URLs with mod rewrite

You will find other articles relevant to this document in these sections:
Richard Lee @ 12:56 am

Ugly URLs…
Querystring style URLs like below are synonymous with dynamic systems

http://www.mysite.com.au/catalog.php?category=cartridges&prod_id=33

While URLs like this are completely functional, there are a few issues with URLs like this you should consider before going live:

  1. The underlying technology is exposed such that script kiddies can inject their own data - such as an sql injection.
  2. The URL contains ampersands which, when unescaped will affect the XHTML compliance of linking websites
  3. Search engines will avoid indexing dynamic pages like this

So without refactoring the application how can you make more freindly URLs? A simple solution for Apache based users is to utilize the mod rewrite module.

mod rewrite to the rescue!

Note: mod_rewrite is not loaded into Apache by default, to do so open your httpd.conf file and uncomment the module load code

Apache’s mod rewrite module can rewrite requested URLs on the fly, meaning you can substitute ugly querystrings with meaningful URLs, which address security issues, compliance and SEO. So how does mod rewrite work?

Basic rewriting

Typically mod rewrite directives are added to a htaccess file in your web root. Here’s a simple example to start with:

RewriteEngine on
RewriteRule ^old_page.html$ new_page.html

This rewrite transparently redirects a request for old_page.html to new_page.html. The first line enables the engine (only required once per htaccess file):

RewriteEngine on

This next line basically tells the server if there is a request matching oldpage.html, then substitute it with new_page.html. The caret ^ and dollar $ sign signify the start and end of the string used for the match.

RewriteRule ^old_page.html$ new_page.html

If you want to actually do a more traditional redirect and show the location of the page in the status bar just add [R] to the end of the RewriteRule.

Easy enough? That was a pretty simple rewrite. The real flexibility of mod rewrite requires some knowledge of Regular Expressions, which can get quite complex. However, the functionality and flexibility Regular Expressions offer in PHP make them well worth learning. So lets take a look at some common rewrites and the expressions used to make them happen.

Unleashing mod rewrite with Regular Expressions

With the help of Regular Expressions we can create RewriteRules which match a set of URLs and redirect them to their actual pages. Consider the products pages in our fictional Shopping Cart app which only vary in category name and product id. We can identify requests for products page by specifically matching the PHP filename, something representing a category name, forward slash, then something representing a product id. And here’s how our rule looks:

RewriteRule ^catalog/([a-zA-Z0-9-])/([0-9]+)/$ /catalog.php?category=$1&prod_id=$2

The parts in square brackets are known as ‘ranges’. In this case where allowing anything in an alphanumeric range (case insensitive) for our category name, then anything in the numeric range for our product id. We’ve also encased these regular expressions in parenthesis so we can ‘back reference’ our matches to pass the values onto our PHP page. Back referencing is done via indexing each set of parenthesises, $1 for our first parenthesises, being our category name, and $2 for our second parenthesises, being our product id.

Well this is the end of my tutorial on how to use mod rewrite to create freindly URLs. If you have any questions please feel free to post a comment. Cheers!

September 5, 2006

Setting up a PHP Development Environment - Part 3: Installing MySQL

You will find other articles relevant to this document in these sections:
Richard Lee @ 11:18 am

Welcome to the final installment of our 3 part series “Setting up a PHP Development Environment”. For those of you who haven’t installed Apache or PHP please checkout Part 1: Installing Apache and Part 2: Installing PHP4 tutorials on our site.
Setting up our Database Server…

For PHP we typically use the MySQL Database. MySQL is a open-source database built primarly for the LAMP stack (Linux, Apache, MySQL, PHP / Perl / Python.) but it’s very flexible, running on more than 20 platforms including Linux, Windows, OS/X, HP-UX, AIX and Netware (source: http://www.mysql.com). In the case of this tutorial we will be setting it to run on Windows XP.

Requirements:

  • PHP 4 or later

(more…)

July 28, 2006

Creating accessible Popup windows

You will find other articles relevant to this document in these sections:
Richard Lee @ 3:09 pm

Often when we create complex pages with lots of content it’s advantageous to use popups for smalls bits of information such a “Help” text. However since the introduction of popup blockers this once simple task has proven to be a tedious job. For example, by default the IE 6 Popup Blocker will block active scripting opening windows without user clicks i.e. using onload() in the body tag for instance. Higher security settings in IE will even prevent scripted popups running off user clicks. So what’s the answer? I came up with a few ideas, my initial thought was to try and detect if my window would be blocked. To do this I would check whether window.open returned a reference to an object:

function popUpWin(url, width, height) {
 
if (window.open) {
window.open(...);
} else {
alert('It appears your browser is blocking popup windows');
}
}


<a href="javascript:popUpWin(disclaimer.htm, 450, 500)">Read Disclaimer</a>

Although sites use this today it’s not a viable solution because it primarily validates the browser and ultimate won’t provide the user with a working link. Instead the user is expected to go off and adjust their browser settings and re-launch the window. Worse still they might potentially miss important information you were trying to provide in that popup (i.e. a disclaimer).
My next more user-orientated solution is what I use today. Employing the JavaScript onclick() function we can call our popUpWin() funcion, and at the same time we can still use our href attribute to supply a normal link if the javascript fails:

function popUpWin(url, width, height) {
 
if (window.open) {
window.open(...);
}
}

<a href="disclaimer.htm" onclick="popUpWin(this.href, 450, 500);return false;" >Read Disclaimer</a>

July 25, 2006

Setting up a PHP Development Environment - Part 2: Installing PHP4

You will find other articles relevant to this document in these sections:
Richard Lee @ 1:14 pm

Part 2 assumes you have a working installation of Apache 2.0 please refer to Part 1: Installing Apache if you haven’t already done so.

Requirements

  • Apache 2.0.x

1. Download the PHP 4.3.x Windows Binary zip package from http://www.php.net/downloads.php .
2. Run a virus scan and an MD5 checksum to verify the integrity of the download, then unzip to the same root directory as your Apache installation. I have Apache under C:\Apache2, so I have unzipped PHP to C:\php4 .

3. Now edit your php.ini file:

Rename C:\php4\php.ini-dist it to php.ini

Open the php.ini file, locate doc_root and set it to whatever your Apache DocumentRoot is set to. Mine is doc_root = “C:\public_html”

Note: Remember that when adding path values in the Apache configuration files on Windows, all backslashes such as c:\directory\file.ext must be converted to forward slashes, as c:/directory/file.ext. A trailing slash may also be necessary for directories.

Scroll down to extension_dir = “” and set it to the location of the ext directory of your PHP installation extension_dir = “C:\php4\extensions”

Since where using windows we also have to set the session save path to an existing directory so we can use PHP’s sessions functions. I recommend using the Window’s temporary directory session.save_path = “c:/windows/temp” OR for Win2k session.save_path = “c:/WINNT/Temp”

4. Next edit your Apache Conf file (hppd.conf )

For PHP 4 add the following line and copy your php4apache2.dll file from the sapi directory into the php4 root directory:

LoadModule php4_module "c:/php/php4apache2.dll"
AddType application/x-httpd-php .php

For PHP 5 do something like this:

LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php

And for both configure the path to the php.ini file:

PHPIniDir "C:/php"

5. Now test your installation - first restarting the Apache server - by creating a simple phpinfo file:

< ?php

phpinfo();

?>

Save this file as phpinfo.php in your webroot C:\Apache2\htdocs and run it through your web browser http://localhost/phpinfo.php. You should now be faced with a printout of your servers settings.

If you do not see this page check to make sure you have restarted Apache by right clicking the Apache monitor in your task bar.

You have now completed your installation of PHP 4.

For more information please refer to php.net’s documentation Apache 2.0.x on Microsoft Windows

July 10, 2006

Setting up a PHP Development Environment - Part 1: Installing Apache

You will find other articles relevant to this document in these sections:
Richard Lee @ 5:23 pm

Welcome to Part 1 of our 3 part series, “Setting up a PHP Development Environment”. Please note we will be using a Windows XP environment for our installation. If you have come here in search of an article on how to install PHP please refer to Part2: Installing PHP4.
Requirements :

  • Windows NT i.e. XP (NT 4.0 has some issues with SP 4 please update to SP 6)
  • TCP/IP networking must be installed and working
  • Intel/AMD Processor (for Win Installer)

Installation & Setup
1. Download the Win32 Binary (MSI Installer) for the Apache HTTP Server package from apache.org .
Note: I am using the 2.0.58 release however 2.2.2 is now available.

2. Run a virus check and MD5 checksum to verify the integrity of the download, then run the installer.
2. Within the Installation Wizard enter the following Server Information, making sure you check the checkboox “For All Users, on Port 80, as a Service” at the bottom.
Apache dialog - Enter Server Info

Network Domain: localhost
Server Name: localhost
Admin Email: (your email)

Note: If you get a Windows Firewall prompt, make sure you select UNBLOCK

3. Install to your local drive. Mine is C:/Apache2 and we will assume this directory in the following installment Part 2: Installing PHP.
4. After installation has completed the Apache Monitor will appear in your Windows task bar. Right-click this and select Start from the menu. The server should start loading.

Apache Monitor

5. After the server has started, open your web browser and visit http://localhost. An Apache test page should come up - You have now successfully installed the Apache Server on your machine.

Apache Test Page

Note: If there is an error, check the Apache Monitor to make sure the server is running, if not check your Firewall to make sure the service isn’t blocked.

6. (Optional) Apache defaults your document root to [drive]:/[Apache]/htdocs , if you would like to specify a different root directory open up the httpd.conf file ([drive]:/[Apache]/conf/httpd.conf) and replace the DocumentRoot with the path to your desired directory.

Follow on Part 2: Installing PHP4

For more information on installing Apache please see apache.org’s documentation Using Apache with Microsoft Windows

June 30, 2006

Creating icons

You will find other articles relevant to this document in these sections:
Richard Lee @ 11:59 am

Recently I had to create some icons for a website I’m working on, so I thought this would be a great opportunity to demonstrate how easy this is.

There seems to be many icon generation tools out their, but it’s just as easy to create icons in Fireworks (or Photoshop).

Creating an icon in Fireworks
Before starting your icon should have a design in mind - remembering there are very few pixels to work with. A good point of reference is the Webdings and Wingdings font-families (I use the Accessories > Character Map).  In this case I am creating an icon for my “Email a Friend” tool.
Sample design

Now I have my design I create a new 16px by 16px document at 72dpi in Fireworks:

Fireworks New Doc

I also want to have some sort of guide when I’m tracing this image so i’ll turn on the grid and set it to 1px by 1px boxes: View > Grid > Edit Grid then View > Grid > Show Grid. I now have a 16px by 16px document divided into 1px squares.

Fireworks 16 x 16 doc

Next I import my design (File > Import), and scale it to the 16px by 16px canvas. You will notice the design is quite aliased, but thats okay, as I only plan to use it as a guide.
Fireworks design import
The last thing I do before starting my icon is create a working-layer (Layers > New Layer) .
Using the Pencil tool (Tip: chose a different outline colour to the design) I start to trace my design to the nearest pixel (re-click pixels to erase them). Every so often I  zoom out to 100% to check how my outline looks to the naked eye. You’ll notice there is a certain level of optical illusion going on, what is actually steps of pixels at 3200% looks like a curved edge to the naked eye.

Finally I have finished the outline of my design. Notice I also decided to fill the background image to create some contrast.
Fireworks icon 3200%And here is the final icon in all it’s glory

Fireworks final icon

For more inspiration I suggest you checkout the IconFactory.com.

June 20, 2006

Turbo Charging Firefox

You will find other articles relevant to this document in these sections:
Richard Lee @ 12:29 pm

Seems everybody wants a turbo nowdays, so what about boosting your browser? This is where pipelining comes into play. You see normally HTTP requests are issued sequentially but with HTTP/1.1 pipelining multiple HTTP requests can made without waiting for the corresponding responses. The client then waits for the responses to arrive in the order in which they were requested. Pipelining the requests can result in a dramatic improvement in page loading times especially over a broadband connection. Below are some settings you may wish to try out (at your own risk). If you are uncomfortable with changing these settings I recommend using the Fasterfox Add-on by Tony Gentilcore.

Enabling Pipelining in Firefox

1.Type “about:config” into the address bar and hit return. Scroll down and look for the following entries:

network.http.pipelining network.http.proxy.pipelining network.http.pipelining.maxrequests

Normally the browser will make one request to a web page at a time. When you enable pipelining it will make several at once, which really speeds up page loading.

2. Alter the entries as follows:

Set “network.http.pipelining” to “true”

Set “network.http.proxy.pipelining” to “true”

Set “network.http.pipelining.maxrequests” to some number like 30. This means it will make 30 requests at once.

3. Lastly right-click anywhere and select New-> Integer. Name it “nglayout.initialpaint.delay” and set its value to “0″. This value is the amount of time the browser waits before it acts on information it receives.

For more information on pipelining please visit the Mozilla FAQ

Next Page »