User-friendly URLs in user profiles

One of the worst things in a web application is the fact of using URLs such as user.php?id=1234 for displaying user profiles. For example, a web application such as Facebook wisely doesn't follow this approach. Instead, it uses something like directory/username for user profiles. This also allows the newly created page to be better indexed by search engine.

I don't have access (of course I don't!) to Facebook's database structure, but I guess that there should be a table which stores my credentials, such as first name and last name (say user_first_name and user_last_name), so the point is creating an unique URL by using these credentials, something like this:

$query = mysql_query("SELECT user_first_name, user_last_name FROM user WHERE user_id='$uid'");
$results = mysql_fetch_array($query);
 $first_name = $results['user_first_name'];
 $last_name = $results['user_last_name'];

$first_name = strtolower($first_name);
$last_name = strtolower($last_name);

$user_ url = $first_name . $last_name;

For the sake of brevity, I've omitted to remove any trailing white-space from both strings, but now you can get the idea. Obviously the variable $uid must be previously set. Anyway, by doing so, you have a good starting point for testing. A problem that will certainly arise is duplicate user credentials, for example when there are one or more users with the same first name or last name. In this case you can use other data from the table, such for example an ID or the username chosen during the registration process.

Leave a Reply

Note: Only a member of this blog may post a comment.