随机用户名

时间:2013-01-11 18:19:34

标签: php random username

好的,希望有人可以帮我解决这个问题。这是情况,我有注册页面,允许用户注册,但只允许用户输入他们的名字,姓氏电子邮件和密码。数据存储在临时用户表中,适用于尚未激活其帐户的用户。

成功注册后,用户可以通过电子邮件激活他们的帐户,一旦激活,数据就会被移动到另一个表并从临时表中删除。(我知道有些人可能不同意这种方法,但这是方法我已经选择了。这一切都很好。

我现在面临的问题是,我有一个用于用户名的字段,这实际上只用于构建url,如下所示:example.com/home/first.last.56但它必须是唯一的当然,用户以后可以在活动后进行更改并登录。

我知道如果我只是允许用户选择他们自己的用户名并且从长远来看可能更简单,这会更容易,但是暂时这不是我想要的。

我考虑过的事情:

使用“临时表”中的唯一行id并将其放在字符串的末尾,如下所示:first.last.56其中first和last是用户的名字和姓,56是来自的唯一行id临时表。这工作正常,但我认为,一旦表增长到1500或2000,那么用户名将很长,即first.last.1597,并且就网址的外观和网址的方式而言有点眼睛疼痛非常用户友好。我想保持这个2位数MAXIMUM。

在保持唯一用户名的同时,有哪些其他方法可以实现此目的?

即使名称与用户真实姓名相似,也可以使用首字母或姓氏的字母来创建用户名。

以下是我的代码到目前为止的样子:

    $new_member_insert_data = array(
        'First_Name' => ucfirst(strtolower($first_name)),
        'Last_Name' => ucfirst(strtolower($last_name)),
        'Email' => $this->input->post($this->session->userdata('email_n_attribute')),
        'Password' => $hash,
        'Confirm_Code' => $confirm_code,
        'IP' => $_SERVER['REMOTE_ADDR']
    );

    $insert = $this->db->insert('Temp_Account', $new_member_insert_data);

    $this->db->select('idAccount');
    $query = $this->db->get_where('Temp_Account', $new_member_insert_data);
    if($query->num_rows() == 1){
        foreach($query->result() as $row){
            $user_name = array('User_Name' => $first_name.'.'.$last_name.'.'.$row->idAccount);
            $this->db->where('idAccount', $row->idAccount);
            $this->db->update('Temp_Account', $user_name);  
        }

    }           

2 个答案:

答案 0 :(得分:0)

首先,我要感谢大家的反馈,并说stackoverflow.com非常棒,拥有最好的社区!如初。

所以经过一些玩弄,几次失败的尝试,以及从上面留下的建议形式的评论是我提出的。

我决定只创建一个处理“随机用户名”创建的私有函数。起初我尝试使用MySQL COUNT()函数编写sql部分,以实现这是不需要的。 (当使用Codeignter使用Active记录时)

相反,您只需查询数据库并检查结果,

    $sql = 'SELECT User_Name FROM Account WHERE User_Name = ?';
    $count = $this->db->query($sql, $user_name);

所以这是random_user_name函数:

private function random_user_name($first_name, $last_name, $data){
    // Generate a random number
    $rand_number = rand(2, 99);
    // Create the username 
    $user_name = array('User_Name' => $first_name.'.'.$last_name.'.'.$rand_number);

    // Check the db for a matching username     
    $sql = 'SELECT User_Name FROM Account WHERE User_Name = ?';
    $count = $this->db->query($sql, $user_name);

    // If a matching username is found run the function again, to create a new username 
    if($count->num_rows() == 1){
            $this->random_user_name($first_name, $last_name, $data);
    }else{

        // Select the row id from the db of the newly created user  
        $this->db->select('idAccount');
        $query = $this->db->get_where('Temp_Account', $data);

        // If we find the user, and we better! update the username field
        if($query->num_rows() == 1){
            foreach($query->result() as $row){  
                $this->db->where('idAccount', $row->idAccount);
                $this->db->update('Temp_Account', $user_name);
            }
        }
    }
}

希望这可以帮助其他人花更多的时间来试图找出原因相当简单的问题,并再次感谢所有人!

答案 1 :(得分:0)

当然,我个人从来没有使用过codeigniter框架,但我会这样做:

$search_user_name = $first_name.'.'.$last_name.'.';
$query = "select count(1) as count from users where User_Name like '{$search_user_name }%'";
//Use whatever DB method you CI uses to run this query
//If possible remove $user_name from the query directly and use bind parameter
//This query will return a number ($count) then you can do the following
$user_name = array('User_Name' => $search_user_name . $count);
//Use the rest of your existing code