User Profile URLs
16 Nov 2010 | Posted in DES551

Most websites that utilise user accounts offer unique URLs for for viewing profiles. For example twitter.com/kylegawley will show my Twitter profile, this works without having to log into the site and browse through a list of users to access a specific profile page. This article explains how to create custom user URLs by modifying your web server’s .htaccess file.
Too Many Pages
The obvious solution to this problem is to generate a new folder and profile page for each user that registers. However, this would be quite cumbersome to program and each page requires its own space on the server, plus it’s a lot of unnecessary work and I am a great fan of keeping things simple.
Codeigniter
I spent a good part of the morning looking into how to achieve this, my research began by downloading the Codeigniter framework and experimenting with Model View Controller, MVC is a development pattern that splits various processes into three individual parts.
The controller handles input from a user/browser – for example; processing a URL, the controller then loads a model which performs various database queries, the results of which are passed to a view. The view is any form of output such as page or RSS feed.
Additional users can be added by adding new classes to the controller, which can subsequently be accessed through a URL. While Codeigniter looks like an awesome framework, I felt that this approach was overkill for what I wanted to achieve, and instead went in search of a more lightweight solution.
If your interesting in experimenting with Codeigniter, I found this great introductory Codeigniter video which explains how to get set up and a very good explanation of the MVC. There are also a number of other great Codeigniter videos available for free on the Nettuts website.
Fancy Redirects
After a bit of digging online, I came across a much simpler solution using server redirects achieved by modifying the server’s .htaccess file. I spent quite a bit of time finding an easy explanation of how to set this up (Apache’s mod_rewrite module is a huge subject). I found the following Youtube video to be the easiest to follow. I’ve also included my own tutorial below, which simplifies this further into a very basic example that can be applied to any site. It’s important to note that your web server must be running Apache for this to work.
Viewing Hidden Files
In order to work with .htaccess files, you need to be able to view hidden files on your system, for OSX users you can do this by opening up the the utilities folder (CMD+SHIFT+U) and starting the Terminal.
Enter the following command:
defaults write com.apple.Finder AppleShowAllFiles TRUE
and then:
killall Finder
To re-hide files again, simply use the following two commands:
defaults write com.apple.Finder AppleShowAllFiles FALSE
killall Finder
Creating The .htaccess File
I don’t know an awful lot about .htaccess files, I am simply using the statements provided in the previous Youtube video. You can download a .txt version of the file here. Simply open this up in a text editor and then save it as .htaccess, then upload to your web server.
The last line in this file is the key element:
RewriteRule ^(.*)$ profile.php?user=$1 [NC]
The redirect location is set to a file called profile.php, passing the appended URL value as a GET argument.
If someone should enter: www.site.com/kyle this is will be redirected to www.site.com/profile.php?user=kyle. The profile.php script can then handle the response and perform user specific output.
The cool thing about this is; the user will never see the redirected URL provided the redirect target is on the same domain.
Handling The Redirect with PHP
I’ve used a very basic example below to demonstrate how to handle the new url – it can be adjusted for your own site and I’ll demonstrate how I’ve applied this to my own prototype later.
echo "Hello " . $_GET['user'];
This single line of code simply gets the username from the URL and echos it back to the browser prepended with a ‘Hello’ string.
Although this is very basic, it demonstrates how you can set up the redirect and obtain the user URL in the PHP file – which can then be used to query a database and show the results, all done with a single PHP script. Of course it’s a good idea to test the user name exists before performing any queries and then re-directing the user to an error page if required.
This is exactly what I’ve done with my prototype, reusing the my previous checkUsername.php script to check the username exists, if it does then I’ve loaded a modified viewImages.php script to display the user images. The reason for the modification is to remove certain functionality such as deleting images so the user can only see the images without being able to modify another users collection.
include 'classes/database_lib.php'; // get username from redirect $username = $_GET['user']; // create new session session_start(); $_SESSION['userProfile'] = $username; // create database connection $protoDB = new database("prototype"); $conn = $protoDB->get_connectionID(); // check current username exists $sql_select = "SELECT user_id FROM user WHERE username = '$username'"; $results = mysql_query($sql_select, $conn) or die(mysql_error()); $rowCount = mysql_num_rows($results); if ($rowCount == 0){ echo "No such user exists"; } else { include 'load-collection.php'; } ?>
The first line includes a new database library I’m working on, I’ve been reusing a lot of clunky code recently in my prototypes so I’ve simplified the database connection into its own class, so I can simply create a new object in each new PHP document to connect to the database (more on this in a separate article).
The next line gets the username from the new redirect URL, and places it into a new session variable called userProfile so it can be accessed from the other pages.
The next two lines create a new database object called $protoDB, passing the name of the database to connect to, the next line returns the connection ID and places it into the variable $conn. You can see from this example how I’ve reduced the process of connecting and selecting a database into two short lines of code.
The rest of the script checks if the username exists, displaying an error message for a non-existant user and loading the load-collection.php file if the user exists.
If you are already registered with my prototype, you can now view any users profile page by accessing it like so: http://kylegawley.co.uk/prototype/kyle – substituting kyle for any username.
References
Anon. Show Hidden Files in Mac OS X. Available: http://osxdaily.com/2009/02/25/show-hidden-files-in-os-x Last accessed 16th Nov 2010.
Haines, B. Everything You Need to Get Started With CodeIgniter. Available: http://net.tutsplus.com/tutorials/php/codeigniter-basics Last accessed 16th Nov 2010.
Shannon, R. Redirecting URLs with Apache’s mod_rewrite. Available: http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html Last accessed 16th Nov 2010.
Way, J. Easy Development with Codeigniter. Available: http://net.tutsplus.com/articles/news/easy-development-with-codeigniter Last accessed 16th Nov 2010.
