- Posts: 224
- Thank you received: 8
Registration: using first part of email as username AND check for duplicates
8 years 6 months ago #293106
by oloccina
Registration: using first part of email as username AND check for duplicates was created by oloccina
Hello,
I follow up from this old post: www.hikashop.com/forum/install-update/88...t-part-of-email.html
Nicolas provided this solution
but now my question is: what happens if there is already a user with that username?
eg. lots of users register with info@somesite.com, 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:
Thank you!
I follow up from this old post: www.hikashop.com/forum/install-update/88...t-part-of-email.html
Nicolas provided this solution
Code:
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 info@somesite.com, 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:
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.
8 years 6 months ago #293111
by nicolas
Replied by nicolas on topic Registration: using first part of email as username AND check for duplicates
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
to
and that should do it.
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
Code:
$form->data['email']
Code:
$this->registerData->username
The following user(s) said Thank You: oloccina
Please Log in or Create an account to join the conversation.
Time to create page: 0.091 seconds