[Skip to main content](#vessel-content)

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

##

- **[oloccina](/forum/user/23273-oloccina "View oloccina's Profile")**
- [![oloccina's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar23273.jpg)](/forum/user/23273-oloccina "View oloccina's Profile") Topic Author
- [ Offline](/forum/user/23273-oloccina)

Less More

- Posts: 224
- Thank you received: 8
-
-

8 years 6 months ago [\#293106](#293106) by [oloccina](/forum/user/23273-oloccina "View oloccina's Profile")  *[Registration: using first part of email as username AND check for duplicates](/forum/27-checkout/894796-registration-using-first-part-of-email-as-username-and-check-for-duplicates#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  but now my question is: what happens if there is already a user with that username? eg. lots of users register with thus you would end up with many &quot;info&quot; 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!")* was created by *[oloccina](/forum/user/23273-oloccina "View oloccina's Profile")*

Hello,
I follow up from this old post: [ www.hikashop.com/forum/install-update/88...t-part-of-email.html](https://www.hikashop.com/forum/install-update/882921-simplified-registration-uses-first-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-&gt;registerData-&gt;username = $this-&gt;registerData-&gt;email; to: $this-&gt;registerData-&gt;username = substr($this-&gt;registerData-&gt;email,0,strpos($this-&gt;registerData-&gt;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:

&lt;?php $user_name = explode('@', $form-&gt;data['email']); $user_name = $user_name[0]; $db = \JFactory::getDBO(); $query = " SELECT &#96;username&#96; FROM &#96;#__users&#96; WHERE &#96;username&#96; LIKE '{$user_name}%' ; "; $db-&gt;setQuery($query); $usernames = $db-&gt;loadColumn(); $temp = $user_name; if ( count($usernames) &gt; 0 ) { while ( in_array( $temp, $usernames ) ) { $temp = $user_name.'_'.rand(1111, 9999); } } $form-&gt;data['user_name'] = $temp; ?&gt;

Thank you!

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI3LWNoZWNrb3V0Lzg5NDc5Ni1yZWdpc3RyYXRpb24tdXNpbmctZmlyc3QtcGFydC1vZi1lbWFpbC1hcy11c2VybmFtZS1hbmQtY2hlY2stZm9yLWR1cGxpY2F0ZXM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86587
- Thank you received: 14326
-
-

8 years 6 months ago [\#293111](#293111) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[Registration: using first part of email as username AND check for duplicates](/forum/27-checkout/894796-registration-using-first-part-of-email-as-username-and-check-for-duplicates#293111 "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 thus you would end up with many &quot;info&quot; 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!")*

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 Code:

$form-&gt;data['email']

to Code:

$this-&gt;registerData-&gt;username

and that should do it.

The following user(s) said Thank You: [oloccina](/forum/user/23273-oloccina "View oloccina's Profile")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtLzI3LWNoZWNrb3V0Lzg5NDc5Ni1yZWdpc3RyYXRpb24tdXNpbmctZmlyc3QtcGFydC1vZi1lbWFpbC1hcy11c2VybmFtZS1hbmQtY2hlY2stZm9yLWR1cGxpY2F0ZXM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.046 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
