Why You Should Always Salt Your Hashes
16 December 2009 | 25 comments | security, database, passwords, programming, webdev
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.
Writing Secure PHP, Part 4
11 September 2008 | 57 comments | code, coding, development, mysql, php, programming, security, tips, tutorial, web, webdesign, webdev
The fourth part of the Writing Secure PHP series, covering cross-site scripting, cross-site request forgery and character encoding security issues.
What Makes a Great Developer?
17 April 2008 | 68 comments | philosophy, programming, software, development, career, tips, job, developer, blog
What makes a truly great developer? Some might say a positive attitude. Some might say a high-sugar, high-caffeine, high-bacon diet. Some might say an absence of sunlight and as many monitors as a desk can support. I say pessimism and laziness are high up the list.
SQL Server Cheat Sheet
20 July 2007 | 68 comments | cheat, cheatsheet, development, mssql, mysql, programming, reference, sql, sql-server, sqlserver, toread
This A4 reference lists the various functions available in Microsoft SQL Server, and demonstrates the creation of stored procedures, triggers and functions.
HTML Cheat Sheet
8 January 2007 | 125 comments | webdev, development, cheatsheet, xhtml, reference, html, web, design, webdesign, tools, css, programming
HTML is the language of the web. It is the semantic support on which websites depend. This A4 reference lists the various tags available to the web designer, as well as a selection of useful character entities, attributes and events.
PHP Querystring Functions
5 December 2006 | 53 comments | links, code, development, url, querystring, reference, php, functions, programming, tips, variable
Adding and removing variables to and from URLs using PHP can be a relatively simple process admittedly, but I have a couple of functions I use often to make the process even less time-consuming.
Add Querystring Variable
A PHP function that will add the querystring variable $key with a value $value to $url. If $key is already specified within $url, it will replace it.
function add_querystring_var($url, $key, $value) {
$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
if (strpos($url, '?') === false) {
return ($url . '?' . $key . '=' . $value);
} else {
return ($url . '&' . $key . '=' . $value);
}
}
Remove Querystring Variable
A PHP function that will remove the variable $key and its value from the given $url.
function remove_querystring_var($url, $key) {
$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
return ($url);
}
Regular Expressions Cheat Sheet (V1)
14 September 2006 | 172 comments | code, howto, cheatsheet, development, regex, regexp, expressions, reference, programming, tips, regular
Regular Expressions are very powerful, and many people find their unusual syntax hard to get to grips with. This A4 reference serves as a guide to regular expression patterns and options.
Microformats Cheat Sheet
9 September 2006 | 49 comments | webdev, cheatsheet, standards, xhtml, html, reference, microformats, programming, web, pdf, hcard, webdesign
Microformats allow us to add more information to our documents on the web, embedding semantic data in web pages that can then be parsed and used in other applications. This reference sheet contains an overview of the most often used microformats.
Ruby on Rails Cheat Sheet
11 January 2006 | 94 comments | rails, cheatsheet, development, tutorial, reference, programming, web, ror, cheatsheets, rubyonrails, ruby, tutorials
A quick reference guide for Ruby on Rails, containing the default directory structure, predefined variables, methods, reserved words and regular expression syntax.
ASP / VBScript Cheat Sheet
1 January 2006 | 42 comments | webdev, reference, cheatsheet, scripting, web, vbscript, programming, webdesign, work, css, asp
A quick reference guide for ASP / VBScript, containing functions, collections, regular expression syntax and other useful bits of information.
JavaScript Cheat Sheet
1 August 2005 | 102 comments | code, webdev, cheatsheet, development, reference, programming, ajax, web, webdesign, javascript, guide
A quick reference guide for JavaScript, listing methods and functions, and including a guide to regular expressions and the XMLHttpRequest object.
Writing Secure PHP, Part 3
27 July 2005 | 53 comments | webdev, security, programming, php, web, tips, work, guide
The third part of the Writing Secure PHP series, covering weak passwords, clients and more advanced topics.
MySQL Cheat Sheet
24 May 2005 | 143 comments | cheatsheet, webdev, development, mysql, reference, programming, web, php, database, guide, sql, webdesign, howto
A quick reference guide for MySQL, including functions (both in MySQL and PHP), data types, and sample queries. Available in PDF and PNG formats.
mod_rewrite Cheat Sheet (V1)
10 May 2005 | 179 comments | howto, webdev, mod_rewrite, development, server, cheatsheet, reference, programming, web, htaccess, apache
A quick reference guide for mod_rewrite, including server variables, flags and regular expression syntax. Also includes examples of commonly-used rules.
CSS Cheat Sheet (V1)
30 April 2005 | 221 comments | webdev, cheatsheet, development, tutorial, reference, tips, programming, design, web, webdesign, css, html, howto
A quick reference guide for CSS, listing selector syntax, properties, units and other useful bits of information.
PHP Cheat Sheet (V1)
22 April 2005 | 247 comments | code, webdev, development, cheatsheet, cheat, reference, programming, php, web, webdesign, tools, cheatsheets
I'm fed up with having to visit php.net to look things up. This A4 cheat sheet is designed to sit by your desk and make your life easier.
Writing Secure PHP, Part 2
22 March 2005 | 37 comments | security, webdev, development, programming, mysql, php, web, work, imported
Learn how to improve your security a little further with the second part of this PHP tutorial.
Password Protect a Directory with .htaccess
15 March 2005 | 98 comments | howto, webdev, security, programming, php, password, protect, sysadmin, htaccess, website, apache, directory, generator
A tutorial explaining how to retrict access to a directory on a web server using .htaccess.
Writing Secure PHP, Part 1
16 July 2004 | 109 comments | code, coding, development, mysql, php, programming, security, tips, tutorial, web, webdesign, webdev
Learn how to avoid some of the most common mistakes in PHP, and so make your sites more secure.
Flesch-Kincaid Reading Level
7 July 2004 | 14 comments | reading, webdev, text, programming, reference, php, writing, readability, language, accessibility, algorithm
Functions to count the number of syllables in a word or sentence, and work out the readability of text.
The Gunning-Fog Index is a measure of text readability based upon sentence length and difficult words in a passage.
Output Caching for Beginners
9 June 2004 | 121 comments | code, cache, webdev, performance, article, development, tutorial, programming, php, web, caching
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.
Ternary Conditionals
2 June 2004 | 28 comments | webdev, development, tutorial, programming, ternary, tips, php, coding
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!
Email Address Validation
1 June 2004 | 203 comments | code, development, email, php, programming, regex, regexp, security, tutorial, validation, webdesign
How to validate email addresses according to ISO standards with PHP.
PHP DateDiff Function
20 April 2004 | 78 comments | webdev, php, datediff, date, script, webdesign, programming
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.
VBScript Date Format Functions
15 March 2004 | 69 comments | scripts, development, reference, programming, vbscript, javascript, time, unix, date, asp, vb, webdesign
Date formatting in VBScript is not quite as powerful as PHP. This function gives you plenty more ways to format dates and times in VBScript with the minimum of effort.
VBScript Regular Expressions
7 November 2003 | 34 comments | code, regex, regexp, reference, programming, asp, vbscript, vb, regular, scripting, expressions, regular-expressions
Regular expression reference and examples for VBScript.