From the category archives:

PHP

Use AJAX Sparingly

June 20, 2007

AJAX

AJAX is a only great technique when used in moderation. It belongs in forms and page widgets, but should never be used for navigational purposes.

Call me a traditionalist, but webpages are pages, not desktop applications. Changing the way a UI component works on your website is unnecessary and confusing for your users.

Normal links shouldn’t change page content without warning and pages shouldn’t extend when you reach the bottom.

Use AJAX sparingly if you want to make a better user experience.

{ 0 comments }

Ruby

Here is a short comparison between Ruby and PHP. The task was to print the location of all HTML links in a webpage using regular expressions.

If anyone knows of a cleaner or more efficient way to do this in Ruby or PHP, please post it in the comments!

Ruby

require 'net/http'

#connect and get the webpage
host = Net::HTTP.new('www.site.com.au', 80)
body = host.get('/index.php', nil ).body

puts "Links found..."

#find link URIs
links = body.scan(/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/)

#print all link URIs
links.each {|id,uri| puts uri}

PHP

<?php

$page = file_get_contents('http://www.site.com.au/index.php');

// find links

preg_match_all('/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/', $page, $links);

// links found

foreach($links[2] as $link)
{
   print "$linkn";
}

?>

{ 0 comments }

MD5 Encryption

May 29, 2007

MD5 Encryption

MD5 is a popular hash function which is often used to encrypt passwords in web applications. In most applications, when a user enters their password, it is encrypted and compared to the one stored in the database. If both md5 hashes match, the user is granted access.

This approach is generally considered to be quite secure for authenticating users. However, it still has it’s weaknesses.

MD5 hashes are vulnerable to dictionary and brute force attacks using rainbow tables; which store millions of passwords and their hashed values.

Which means if your database is compromised there’s a good chance that your passwords can be recovered by an experienced cracker.

Here are a few tips for protecting your passwords…

Also make sure you’re using SSL if your web application has any importance. It’s extremely easy to intercept passwords using packet sniffers.

{ 0 comments }

Data Visualization

May 24, 2007

Data visualisation isn’t a new concept. For decades, scientists and geographers have been been relying on data visualisations to present their huge amounts of data.

Only recently has it started gaining interest on the web, and I believe we have Digg Labs to thank for that.

Since releasing their API, a number of great visualisations have surfaced. Two of my favourites are Digg RADAR and Digg Swarm.

User product Clusters

There are also some great visualizations in the networking industry. You can even monitor your network traffic in video game style with Netcosm.

Visit Visual Complexity for hundreds more examples.

I thought it would be fun to make a visualization of my own, and here’s what I came up with.

User product Clusters

Each “star” represents a product, and is grouped in a cluster which represents the user it belongs to. The end result is an interesting star field.

Obviously it’s not as impressive as the Wikipedia activity image mosaic, but I had fun making it.

{ 0 comments }

A lot of PHP beginners tend to steer clear of regular expressions,
sometimes resorting to using an ugly mixture of str_replace and explode.

That’s not surprising, regular expressions can be intimidating. Take a look at this pattern for validating an email address…

^[a-zA-Z][w.-]*[a-zA-Z0-9]@[a-zA-Z0-9][w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z]$

Fortunately, we can accomplish most programming tasks with much easier patterns.

This isn’t meant to be a comprehensive guide, so I’ll keep it very simple and just demonstrate how easy it is to grab the number of new links from dzone‘s homepage.

Here is the HTML source code surrounding the new links number.

<li><a href="/links/queue.html" >New links (217)</a></li>

And here is the PHP code to fetch the number of new links.


<?php
  // fetch the source of the homepage
  $dzone = file_get_contents('http://www.dzone.com/');

  // find matches for the pattern
  preg_match('/<li><a href="/links/queue.html" >New links ((.*))</a></li>/', $dzone, $matches);

  // print result
  echo $matches[1];
?>

We use PHP’s preg_match function to search in a string for a pattern.

As you can see, the pattern is just the surrounding HTML with all /)( characters escaped and “(.*)” replacing the number. Too easy.

{ 0 comments }

Of course, there is no “better” language, each language has its own strengths and weaknesses. When starting a new project, you need to chose the right tool for the job.

For me, the right tool is still PHP.

If I was starting a web development business tomorrow morning, I wouldn’t be writing my codebase in Python or Ruby (although they are nice languages), I would chose PHP for the following reasons:

  1. Everyone knows PHP
    PHP experts are never hard to find, which means a greater selection of job candidates.
  2. There are more PHP resources available

    There are many PHP commmunity websites, tutorials, frameworks, classes and articles.
  3. PHP is well supported
    Most linux web hosts supports PHP4, and more are starting to support PHP5.

When you’re betting your business on a language, you don’t want to take any risks. At the moment I see PHP as still the safest choice.

{ 0 comments }