MyBB Community Forums

Full Version: Rename a username if it is already exists.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to rename a username if it is already exists.. By the way i'm implementing an auto register.

For example I have a variable which holds the username of a user (preferably his/her firstname).

--> $theusername = "Peter";

Now I would like to know first if ($theusername) is existing on the database. If its available, the user will auto register to the forum. If not, it will rename the username adding number to its username. So this is the process...

--> if ($theusername already exists){ //dont know the code here (any help?)
       //register - i have my own code here
      }
      else 
      { 
       echo "Your username already exists, we will rename it to register you successfully.";
       //rename the username (e.g. Peter1 or Peter2 if Peter1 already exists Peter3 and so on.) - I don't know the code here (any help?)
       //register  - i have my own code here
      }

I'm experimenting this auto register but I can't start coding. Anyway, any idea will be appreciated. Thank you!
admin panel >> users & groups >> users >> find users --> enter your user name and proceed to edit profile
there you can change the user name (put required new user name) and save the profile

or use this plugin: http://community.mybb.com/mods.php?action=view&pid=76
(05-11-2016, 05:10 AM)Donald_Duck Wrote: [ -> ]admin panel >> users & groups >> users >> find users --> enter your user name and proceed to edit profile
there you can change the user name (put required new user name) and save the profile

or use this plugin: http://community.mybb.com/mods.php?action=view&pid=76

Thanks for this. But what i need is a customize code.
You can concat the username with random value
$randomString = md5(uniqid(rand(), true));
(05-11-2016, 05:33 AM)Donald_Duck Wrote: [ -> ]You can concat the username with random value
$randomString = md5(uniqid(rand(), true));

I would like to use that random value. 
I try to code and this is what I ended.
$query = $db->simple_select("users", "COUNT(username) as usernames", "username ='".$db->$theusername ."'");

if($db->fetch_field($query, "usernames") > 0) //check if it is existing
     {
      
       $suffix= md5(uniqid(rand(), true));

        $username = $theusername + $suffix; 

	}
     else 
     {
	 $username = $theusername;
      }

//register user  - i have my own code here

What do you think about this? any improvement on my code?