Say I have a social network which accepts:
short url’s using the nickname: e.g. -> http://www.mynetwork.com/john.doe
256 bit AES hashes: e.g. -> http://www.mynetwork.com/23fdgfdjhdbfuyviofhedfbncxgersbzc34…
i want to route them to a single controller like ‘Profile’ wherein:
the short url’s version goes to a function like: fetch($nick) in which,
i hash the nick to aes, then make a redirect to call the a function: view($hash)
the view function is the one that actually has the logic for something like displaying that user’s profile.
this ensures that whatever the case in which url i choose to visit a profile, whether short url or hash, i always end up getting redirected to the hash url call…
how can i do that? i am thinking it’s something to do with routes but i am a little lost on how to go about with it…
to make it short, accessing:
http://www.mynetwork.com/john.doe redirects to: http://www.mynetwork.com/23fdgfdjhdbfuyviofhedfbncxgersbzc34 and displays that user’s profile
and of course, directly accessing http://www.mynetwork.com/23fdgfdjhdbfuyviofhedfbncxgersbzc34 will simply display the user’s profile
If you’re not willing to use different URL’s for the different functions, like this:
Then, you’ll have to write a function that can diffrentiate between a nick and a hash. In your examples, CodeIgniter sees this for both, nick and hash:
You could do something like this:
The controller:
You will also have to edit your URI routing for CodeIgniter so it sends
nickOrHashas an argument toindexand does not search for a function callednickOrHashunder theview controller(http://codeigniter.com/user_guide/general/routing.html)In the above code, replace
length_of_hashwith the length a 256 bit AES hash will always haveEDIT:
Another, more reliable way of determining whether the argument is a nick or hash, could be:
viewByHash($arg)$arg,and check database again if user with the new hash exists. If exists callviewByHash(newHash)with the calculated hash.error 404Routing:
If a user requests url
mynetwork.com/view/arg, codeIgniter will callindex()method inview controllerpassing the last part of the user’s request URL as the argument.You can change it to something like this, if you want to check by hash directly:
If above function fails, then check by username, etc etc