Cheat Sheets
mod_rewrite (V2)
HTML
Microformats
ASP / VBScript
HTML Character Entities
JavaScript
MySQL
mod_rewrite (V1)
CSS (V1)
PHP (V1)
RGB Hex Colour Chart
Articles
Writing Secure PHP, Part 4
The fourth part of the Writing Secure PHP series, covering cross-site scripting, cross-site request forgery and character encoding security issues.
Writing Secure PHP, Part 3
The third part of the Writing Secure PHP series, covering weak passwords, clients and more advanced topics.
Writing Secure PHP, Part 2
Learn how to improve your security a little further with the second part of this PHP tutorial.
Writing Secure PHP, Part 1
Learn how to avoid some of the most common mistakes in PHP, and so make your sites more secure.
The Box Model For Beginners
An explanation of what the box model is and how it is treated by different user agents.
Output Caching for Beginners
High-traffic sites can often benefit from caching of pages, to save processing of the same data over and over again. This caching tutorial runs through the basics of file caching in PHP.
HTTP Status Codes for Beginners
All valid HTTP 1.1 Status Codes simply explained.
Blog and Lab
Why You Should Always Salt Your Hashes
Hashes are used almost everywhere on the web, behind the scenes, to protect your passwords. Learn why it's important to always add salt to your hashes.
Click here to read this post »
16 December 2009 | 18 comments | security, database, passwords, programming, webdev
Rex Swain HTTP Viewer Bookmarklet
Rex Swain's HTTP Viewer is a great tool for checking HTTP status codes, redirection, and so on. I've been unable to find a bookmarklet, though, for sending the URL I am viewing to the viewer automatically, so I put one together:
Rex Swain's HTTP Viewer
RSS to iCal
I have been looking for a way to convert the BBC weather feed for my area to iCal, so I can subscribe to it. It's date-based, after all, and RSS never seemed to me to be an appropriate format for subscribing to weather information. iCal always struck me as being "better" for that purpose. Of course, the BBC only have an RSS feed for local weather. What I needed was a converter.
After some hunting, I discovered that Dean Sanvitale had written a PHP script to convert RSS feeds to iCal format. However, his site (codent.com) appears to be long since abandoned and the script is no longer available from there. Fortunately, the Wayback Machine did have a copy. Dean originally released the script under a Creative Commons License which, fortunately, allows me to make the script available to download from this site (note: the script is available from this site under the same license).
So, if you're looking for a way to convert an RSS feed to iCal, this PHP script will do the job. Thanks Dean!
Source: rss2ical.txt
19 October 2006 | 15 comments | rss, weather, php, bbc, ical, convert, tools, web, webdev, code, rss2ical
Internet Country Codes
A list of country level domain names, ordered by country or domain name.
Click here to read this post »
07 October 2005 | 21 comments | cheatsheet, webdev, reference, domain, domains, tld, codes, dns, internet, resources, country
Block Referrer Spam (Updated)
Referrer spam is becoming increasingly common. At best, it will only render your log files useless. At worst, it can cause your site to be dropped by search engines and your running costs to skyrocket. Here's how to block spurious referrers.
Click here to read this post »
14 September 2005 | 41 comments | howto, webdev, server, spam, referrer, wordpress, htaccess, admin, apache
Block Prefetching
Mozilla and Google's prefetching functions are a nice addition to browser technology in many ways. Unsurprisingly, they are not very well thought through. The main two problems with the prefetching idea are that it messes with log files and it means every link on a page could potentially be followed despite the consequences (dangerous in a site administration context).
It appears from the FAQ that Google only intends their accelerator to prefetch specific pages, that have been specified with the <link> tag. However, many people are claiming that normal links have been prefetched.
To prevent prefetching of a page is simple: add the following PHP to the page you do not want prefetched:
if ((isset($_SERVER['HTTP_X_MOZ'])) && ($_SERVER['HTTP_X_MOZ'] == 'prefetch')) {
// This is a prefetch request. Block it.
header('HTTP/1.0 403 Forbidden');
echo '403: Forbidden<br><br>Prefetching not allowed here.';
die();
}
This will serve a "forbidden" header to the prefetcher. Normal browsing should be unaffected.
20 April 2005 | 3 comments | webdev, block, mozilla, prefetching, google, reference, php
Password Protect a Directory with .htaccess
A tutorial explaining how to retrict access to a directory on a web server using .htaccess.
Click here to read this post »
15 March 2005 | 84 comments | howto, webdev, security, programming, php, password, protect, sysadmin, htaccess, website, apache, directory, generator
Preload Images with CSS
How to preload images using CSS and so avoid delays with rollover effects.
Click here to read this post »
23 December 2004 | 57 comments | code, rollover, webdev, howto, preload, web, design, image, webdesign, images, css
Flesch-Kincaid Reading Level
Functions to count the number of syllables in a word or sentence, and work out the readability of text.
Click here to read this post »
07 July 2004 | 14 comments | reading, webdev, text, programming, reference, php, writing, readability, language, accessibility, algorithm
28k and 56k Modem Emulator
Regardless of your connection speed, this will show you how your site loads on PCs with older connections.
Click here to read this post »
29 June 2004 | 65 comments | webdev, development, web, design, usability, modem, webdesign, tools, accessibility, optimization, testing
Ternary Conditionals
Ternary conditionals (using the "ternary operator", sometimes known as the "trinary operator") are a part of PHP that many simply steer clear of, despite their usefulness. They can save a great deal of time when writing code and can make for much easier code to read and edit later on. They look strange to many people though, which might explain why they are not as widely used as they could be.
Consider a normal conditional statement, like the following. It begins by evaluating a condition. If that condition is true, it follows one path. Sometimes, an alternate path is specified if the condition is not true (the 'else' section). Sometimes, you can have a list of several possible conditions in a row (using 'if ... elseif ... else' or 'switch ... case').
if (condition) {
variable = value-if-true;
} else {
variable = value-if-false;
}
However, a simple situation like the above is a perfect candidate to convert to a ternary conditional. You have one condition, and if it is true, the variable is given a certain value - if false, a different value. A ternary conditional can accomplish the same thing, concatenating it into one simple line of code.
variable = (condition) ? value-if-true : value-if-false;
Ternary conditionals take the above form. You do not necessarily need to have a "variable = " section (as you will see later on), but usually that is what this is used for. The above does exactly the same thing as the 'if ... else' statement earlier. If the condition evaluates to true, the variable will be assigned the value in the "value-if-true" section, otherwise it will receive the "value-if-false" value.
In practice, you could use the ternary conditional to, for example, greet a user depending on whether it is currently morning or afternoon. Using traditional code ('if ... else'), you might write something like this:
if (date("G") < 12) {
echo 'Good morning';
} else {
echo 'Good afternoon';
}
The same statement, using a ternary conditional, would look like this:
echo (date("G") < 12) ? 'Good morning' : 'Good afternoon';
Note that in this example, we've used "echo", rather than assigning a value to a variable. The above is exactly the same as this, which does make use of a variable:
$greeting = (date("G") < 12) ? 'Good morning' : 'Good afternoon';
echo $greeting;
Another situation in which I often use ternary conditionals is when displaying rows of data. It can often be much easier for a user to see what is going in if the rows alternate background colour, and the following code can be useful for that:
$i = 1;
echo '<table>';
while ($data = mysql_fetch_array($result)) {
echo ' <tr>';
echo ' <td bgcolor="';
echo (($i % 2) == 0) ? '#eee' : '#ddd' ;
echo '">';
echo $data['field'];
echo ' </td>';
echo ' </tr>';
$i++;
}
echo '</table>';
The above code will cycle through a result set, displaying each item in a new row. The background colour of the row will alternate between shades of grey, controlled by the ternary conditional on the bold line.
Ternary conditionals make for tidier code. Use them - if not for yourself, then for whoever is going to end up editing your scripts!
02 June 2004 | 27 comments | webdev, development, tutorial, programming, ternary, tips, php, coding
PHP DateDiff Function
VBScript's DateDiff function is a powerful way to express differences between dates, and PHP lacks a similar function. Here's a replica of VBScript's DateDiff function in PHP.
Click here to read this post »
20 April 2004 | 74 comments | webdev, php, datediff, date, script, webdesign, programming
US States Select Box
A populated select list (input box) with the states of the USA as options.
Click here to read this post »
14 February 2004 | 26 comments | select, webdev, development, html, reference, resource, design, forms, webdesign, states, resources
