views:

779

answers:

6

I have a signup page on my website where a user must provide a email address and password only.

I want to be able to create a username for this user automatically by using the first part of the email provided;

User supplies gordon@yourdomain.com, i want to make username 'gordon'

I don't need explanation on how to create form or submission of data to database, just the code to extract data from email provided, and if necessary, if duplicate occurs add number to end.

Hope this makes sense, seems like a basic function but couldn't find examples of it anywhere on net!

+9  A: 

This is not a good idea, just use their full email address. The following email addresses could be different people, but they will become the same under your system.

[email protected]
[email protected]

Adding a number to the end will make the user remember something unique to your system and cause much confusion on their end.

sirlancelot
Thanks for your quick reply....I am not using the username for a user to login, they will still use there email address to achieve this (as mentioned below email address in an excellent unique id). The reason i want to extract part of email and use as username is more for profile creation. I am designing a social network so if the user meets someone they can tell them there username or email address to find
Then you should go the extra effort of letting the user choose a username; you are going to need to check for conflicts anyway
Manrico Corazzi
I want the user registered that is my main objective here, and I want it seen to be very easy. Once the user is registered they can change there profile to whatever they prefer very easily.The other reason for doing this, is that I want to be able for users to create an account with me by simply emailing a particular address.
@Luke: Your still going to run into the samename problem sirlancelot highlighted. Which means you would have to check for dupes and potentially change it. What you're describing is not normal behavior. It's better to leave the profile name blank and let them fill it out if they want that feature.
Chris Lively
+1  A: 

Something like:

$username = left($email, stripos($email, '@'));

should do. You may want to learn regular expressions for these kinds of task.

Then you add the counter:

function countOccurrences($name)
{
    $con = mysql_connect(___, ___, ___);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db(___, $con);

    $result = mysql_query("
        SELECT COUNT(*) AS countOccurrences
        FROM users
        WHERE username LIKE '" . mysql_real_escape_string($name, $con) . "%'
    ");

    $row = mysql_fetch_array($result);

    $number = $row['countOccurrences'];

    mysql_close($con);

    return $number;

}

and then:

$countUsers = countOccurrences($username);
if ($countUsers>0)    
{    
      $username = $username . $countUsers;
}

IMPORTANT: Consider using the whole email as username: you don't want [email protected] to be considered equal to [email protected]

NOTE: example code counts gordon, gordon1, gordon2 but gordonbah, gordonq, gordonxxx too

NOTE: this is pretty rough, and should not be considered best PHP practice; it's just to give the general idea

Manrico Corazzi
You're not addressing the behavior OP requested for duplication cases.
chaos
That's because I misread that -- thanks, will add suggestions right away
Manrico Corazzi
Ugh, you didn't escape $name before putting it into mysql... *very bad*
Ian
Fantastic thanks so much to everyone.I want to clarify to everyone what I am trying to achieve. I want to make it extremely quick and easy for a user to join my network. Therefor I only request email address and password to join. For security reasons i use a validation process for the email. The other factor I want to achieve when a user signs up is to be able to create a profile with all the default settings and where the profile unique id is the username generated. Hope this makes a bit more sense
As I said: no best practices, here, just suggestions for a possible solution. Of course sanitizing the query parameter is mandatory, among the 1.000.000 other things (like providing a template for opening/closing the connections, an object model for the entities, error handling, etc.)
Manrico Corazzi
That's just one of those things that makes my head explode because so many people omit escaping their variables which is such a huge security problem..
Ian
@chaos: thanks for adding string escaping code@Ian: I agree with you completely; as I said I just focused on the solution of the problem, since I thought that accessory code would be handled correctly by Luke on his own
Manrico Corazzi
A: 
<?php
 preg_match('/[^@]+)@/',$email,$matches);
 $username = $matches[1];
 check_availability($username);
?>
Piskvor
+1  A: 

Agreed, you're stripping a necessarily unique ID into a non-unique ID. Unless you want to add some sort of handling to add a number to the username or something. If that's what you really want to do, this should set $username to the stuff before the email address:

<?php
   $username = preg_replace('/([^@]*).*/', '$1', $email);
?>
C. Alan Zoppa
A: 

Should suit your needs :D

$username = substr($username, 0, strpos($username, '@'));

$username = mysql_real_escape_string($username);

$result = mysql_query('SELECT `username` FROM `users` WHERE `username` = \'' . $username . '%\';');

if (mysql_num_rows($result) > 0) {

   $i = 0;

   while ($name_arr = mysql_fetch_assoc($result)) {

      $name = $name_arr['username'];       

      $after = substr($name, strlen($username));

      if (ctype_digit($after)) {

         if (($after = (int) $after) > $i) {

            $i = $after;

         }

      }

   }

   if ($i > 0) {
      $username .= $i;
   }

}
Ozzy
A: 

Why not I can't sign space?