Limit username restrictions


Ok, so basically, this is a helpful snippit for people who only want their visitors to be able to register with the following characters, a-z, A-Z, 0-9,_, and -.

Find in register.php:
PHP Code
  1. $adduser = mysql_query("INSERT INTO `members` (`username`, `password`, `email`, `userlevel`) VALUES('$username','$password','$email','2')");
  2. echo "You are now registered,<br><br>You can now loggin to your account";


Before it, add this:
PHP Code
  1. if (ereg("^[a-zA-Z0-9_]{3,16}$",$username)) {

after it add this:
PHP Code
  1. }else{
  2. echo "You're username contains invalid characters.";
  3. }


and viola, you're done.

So, your final product should be something along these lines:

PHP Code
  1. if (ereg("^[a-zA-Z0-9_]{3,16}$",$username)) {
  2. $valid = true;
  3. $adduser = mysql_query("INSERT INTO `members` (`username`, `password`, `email`, `userlevel`,`ip`, `signupdate`) VALUES('$username','$password','$email','2','$ip','$datex')");
  4. echo "<h2>Success!</h2> You are now registered, $username. You can now <a href='https://avidpictures.com/beta/login.php>login</a> to your account!</a>";
  5. }else{
  6. echo "Invalid username";
  7. }


Any questions or help, post and I'll do my best to help you. Remember, this also works for other input fields!
UrbanTwitch's Avatar
Views:
11,751
Rating:
Posted on Tuesday 18th November 2008 at 11:44 PM
Jakkkay
Jakkkay's Avatar
Ill pay 20 bucks for a usersystem already made and can easily get installed by me :D if you ware interested email me at legitimateproductions@live.ca

Or add :D
Posted on Thursday 6th November 2008 at 01:16 AM
Dava
Dava's Avatar
for those who want preg_match just use the following

Code
if (preg_match("/^[a-zA-Z0-9_]{3,16}$/",$username)) {
Posted on Wednesday 8th October 2008 at 01:27 PM
UrbanTwitch
UrbanTwitch's Avatar
Show me your code.
Posted on Wednesday 8th October 2008 at 07:01 AM
jambomb
jambomb's Avatar
Doesnt seem to work ?

I tryd my username like %$%£ $%£%^*((^ lol
Posted on Wednesday 8th October 2008 at 05:28 AM
Matt
Matt's Avatar
ereg is slow, why dont you use preg_match?