Registration: using first part of email as username AND check for duplicates

  • Posts: 224
  • Thank you received: 8
6 years 6 months ago #293106

Hello,
I follow up from this old post: www.hikashop.com/forum/install-update/88...t-part-of-email.html

Nicolas provided this solution

It uses the full email address for the username.
It can however be changed quite easily by changing the line:
$this->registerData->username = $this->registerData->email;
to:
$this->registerData->username = substr($this->registerData->email,0,strpos($this->registerData->email,'@'));
in the file administrator/components/com_hikashop/classes/user.php

but now my question is: what happens if there is already a user with that username?
eg. lots of users register with This email address is being protected from spambots. You need JavaScript enabled to view it., thus you would end up with many "info" username wich would cause errors in the Joomla registration process

can the suggested code be easily changed to check for duplicates and in case add random numbers after the username?
Or it already does that?

On some forms by Chronoengine.com I have on the site, they suggested this code:
<?php
$user_name = explode('@', $form->data['email']);
$user_name = $user_name[0];
$db = \JFactory::getDBO();
$query = "
    SELECT `username`
        FROM `#__users`
        WHERE `username` LIKE '{$user_name}%' ;
";
$db->setQuery($query);
$usernames = $db->loadColumn();
$temp = $user_name;
if ( count($usernames) > 0 ) {
  while ( in_array( $temp, $usernames ) ) {
    $temp = $user_name.'_'.rand(1111, 9999);
  }
}
$form->data['user_name'] = $temp;
?>

Thank you!

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
6 years 6 months ago #293111

Hi,

When the username (entered by the user) is already used, the registration is refused and the user is back on the registration form with all of its information already entered and an error message so that he can change the username (or email in that case) to something else.
I believe it's better than generating a random username. If you do that, then the customer doesn't know about the username you randomly generated (and he might not see it in the email notification (which you might not even send)), and he might not like it and prefer something he can easily remember.
So i would advise against doing something like that. I believe it would do more harm than good and waist your time.

Now if you want to do that, you could use that code and just change

$form->data['email']
to
$this->registerData->username
and that should do it.

The following user(s) said Thank You: oloccina

Please Log in or Create an account to join the conversation.

Time to create page: 0.054 seconds
Powered by Kunena Forum