Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to make sure username isn't a reserved word?

Tags:

validation

Let's say I'm building a web application whose user pages can be found at http://example.com/NAME. What's the best way to make sure the username doesn't conflict with a reserved word (e.g. 'about', 'contact', etc.)? I can think of two ways:

  • Maintain a list somewhere in my code. This is great and all, but means I have another piece of code I have to edit if I decide to, say, change the "about" page to "aboutus".
  • Request the URI (e.g. http://example.com/someusername) and check if it exists (doesn't return a 404). This feels kind of like a hack, but on the other hand it does exactly what it's supposed to do. On the other hand, I can't reserve anything without making a page for it.

What would be the best way to go about this? Manual validation of usernames is not an option. Thanks!

EDIT: I forgot to mention, the username has to go at the root, like this:

http://example.com/USERNAME

Not like this:

http://example.com/users/USERNAME

Hence why I'm asking this question. This is for technical reasons, don't ask.

like image 564
Sasha Chedygov Avatar asked Jun 03 '09 08:06

Sasha Chedygov


People also ask

Should username be one word?

In our earlier example, "John Smith" is the user name, and "smitj" is the username used with his password. The Microsoft Manual of Style 4th Edition states that the user name should be two words, unless describing a label in the user interface.

Can we use reserved words as identifier?

In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a reserved word may have no user-defined meaning.


3 Answers

I would strongly suggest using a unique path like http://example.com/users/NAME instead. Otherwise, what are you going to do if you want to add a reserved word, but a user has already taken it as their user name? You'll end up with all kinds of potential migration problems down the track.

Alternatively, if you must have something that goes straight off http://example.com/, could you possibly prefix all user names? So that user jerryjvl would translate to link http://example.com/user_jerryjvl?

If there is really no other possible solution, then I'd say either check user names against whatever data source determines what the 'reserved words' are, or make a lookup file / table / structure somewhere that contains all the reserved words.

like image 50
jerryjvl Avatar answered Oct 18 '22 08:10

jerryjvl


In the interest of completeness, if you can't change the routing. Another possibility is to have your user routes and your non-user routes have a programmatic distinction. For example, if you appended a '_' to the end of each of your user routes, then you can make sure that users are located at: http://example.com/NAME_ and the other route would never end in '_'

like image 3
Sean Avatar answered Oct 18 '22 07:10

Sean


How about changing your routing scheme so that users are at example.com/users/NAME ?

like image 2
spender Avatar answered Oct 18 '22 07:10

spender