【问题标题】:Random User Name随机用户名
【发布时间】:2013-01-11 18:19:34
【问题描述】:

情况就是这样,我有注册页面,允许用户注册,但只允许用户输入他们的名字、姓氏电子邮件和密码。数据存储在临时用户表中,供尚未激活帐户的用户使用。

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

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

我知道如果我只允许用户选择他们自己的用户名会更容易,从长远来看可能更简单,但目前我不想这样做。

我想过要做的事情:

使用“临时表”中的唯一行 ID 并将其放在字符串的末尾,如下所示:first.last.56 其中 first 和 last 是用户的名字和姓氏,56 是来自临时表。这行得通,但我认为一旦表格增长到 1500 或 2000,那么用户名就会很长,即 first.last.1597 并且就 url 的外观和 url 的方式而言有点眼痛不是非常用户友好。我希望将其保持在 2 位数的最大值。

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

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

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

    $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);  
        }
    
    }           

【问题讨论】:

  • 你能拥有 1000 多个 John Does 吗?
  • 检查是否存在首尾相同的记录?如果是这样,计算有多少并加 1。第一个 John Doe = john.doe.1 第二个 John Doe = john.doe.2
  • 您还可以将 ID 转换为 Base64 而不是 Base10,这样可以使其更短。我也同意上述观点 - 在添加任何数字之前检查它是否重复。
  • 抱歉让我等了很久才回复,在我上班前发布。 @Pitchinnate 是的,这似乎是处理这个问题的一个很好的逻辑方法,因为我希望大多数用户在登录后更改他们的用户名。如果不是太多,你能举一个 mysql 外观的例子吗?我正在使用 codeigniter 框架。谢谢!
  • @SalmanA 可能性不大,但是我认为在处理数据库时应始终避免任何发生冲突的机会,以免发生冲突。似乎也是一种很好的编码实践,以确保它不是可能的。

标签: php random


【解决方案1】:

首先,我要感谢大家的反馈,并说 stackoverflow.com 很棒,拥有最好的社区!永远。

所以在做了一些玩弄之后,几次失败的尝试,以及上面留下的来自 cmets 的建议,这就是我想出的。

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

相反,您可以像这样简单地查询数据库并检查结果,

    $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);
            }
        }
    }
}

希望这可以帮助其他人花费不必要的时间来试图弄清楚原来是一个相当简单的问题,再次感谢大家!

【讨论】:

  • 一如既往,我愿意接受更好或改进的解决方案。
【解决方案2】:

当然,我个人从未使用过 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

【讨论】:

  • 澄清一下,基本上我会使用统计的数字作为用户编号,而不是随机生成的数字?
  • 正确,它将是您运行$query时收到的值
  • 抱歉,迟来的回复,学校和工作!大声笑,这似乎是一个好方法,实际上可能比使用随机数方法快一点。有机会我会试一试。谢谢!
猜你喜欢
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
相关资源
最近更新 更多