简体   繁体   English

注册页面,电子邮件或密码是否存在

[英]Registration page, email or password exist check

I've written a registration page in PHP using prepared statements. 我已经使用准备好的语句在PHP中编写了一个注册页面。 My question is a fairly simple one. 我的问题很简单。 When a user registers I want to make sure that they aren't registering under the same username or email as another person. 用户注册时,我想确保他们没有使用与另一个人相同的用户名或电子邮件进行注册。 Therefore as part of my verification procedure I run two queries, one to check if the email exists and one to check if the username exists. 因此,作为验证过程的一部分,我运行两个查询,一个查询邮件是否存在,另一个查询用户名是否存在。 My question is, is there a better method? 我的问题是,有没有更好的方法? I feel that running more that one query could somehow sacrifice some performance. 我觉得运行一个查询可能会以某种方式牺牲一些性能。 Will performance even be noticably affected by such a query? 这样的查询会显着影响性能吗? I realise that I could run "SELECT * FROM table WHERE username = ? or email = ?", but I would like the user to know what they got wrong rather than just a generic "Either your username or email is currently being used, feel free to use trial and error to find which it is". 我意识到我可以运行"SELECT * FROM table WHERE username = ? or email = ?",但我希望用户知道他们出了什么问题,而不仅仅是一个通用的“当前正在使用您的用户名或电子邮件,可以随意使用试验和错误来找到它是什么。” It's probably me just being paranoid about speed, but I was just interested to see what everyone else's opinion on the matter is. 可能只是我对速度感到偏执,但我只是想看看其他人对此事的看法。

Run your query with OR and check the returned results to report what exactly you've found. 使用OR运行查询,并检查返回的结果以报告您所发现的确切信息。

However, depending on database internals and indexing, two separate queries might be even faster than OR'ed together. 但是,取决于数据库的内部结构和索引编制,两个单独的查询可能比“或”在一起的速度更快。 You'd better benchmark all cases (found one, found both, found none) for definite answer about your environment. 您最好对所有情况(发现一个,发现两个,都找不到)进行基准测试,以获取有关您的环境的明确答案。

In database where you are keeping user's information make the username and email entity unique. 在保留用户信息的数据库中,使用户名和电子邮件实体唯一。 Like primary key. 像主键一样。

Your query is good; 您的查询很好; You will have 1 or 2 result; 您将得到1或2个结果; 2 if a registration exists with only the email or only the nickname; 2,如果注册只包含电子邮件或昵称; 1 if a registration has the email and nickname; 1如果注册具有电子邮件和昵称; use JS and specify the reasons on the right of the input fields 使用JS并在输入字段的右侧指定原因

Well, I suppose that would be the simplest way: 好吧,我想那是最简单的方法:

SELECT IF(username = :username_to_check, 1, 0) AS dup_username
  FROM users
 WHERE username = :username_to_check OR email = :email_to_check

Then you check: 1) the number of rows - if it's 0, no duplicates were found; 然后检查:1)行数-如果为0,则没有发现重复; 2) the dup_username flag of your result, if rows count > 0. 2)如果行数> 0,则显示结果的dup_username标志。

Here's an example to play with. 这是一个例子

I dont think its bad for performance 我认为这不会影响性能

if you have select query that is getting one row from table; 如果您有从表中获取一行的选择查询; of lets say thousands rows ; 可以说数千行; then the impact is not to be mentioned at all.. 那么根本就不会提及影响。

What effect your performance is join queries which needs a lot of resources which isn't happening at your case ... 对您的性能产​​生什么影响的是联接查询,它需要大量资源,而这种情况在您的情况下是不会发生的...

I believe its safe to use it separately, we've been doing it on our website which currently have over one million users 我相信它可以安全地单独使用,我们一直在我们网站上进行此操作,该网站目前拥有超过100万用户

also try to run this query in your mysql server and see how long it takes this can help (especially for more complicated queries ) 还尝试在您的mysql服务器中运行此查询,并查看此查询需要多长时间(特别是对于更复杂的查询)

    $result = mysql_query("SELECT * FROM table1 WHERE Username = ''", $link);
    $num_rows = mysql_num_rows($result);

    $result2 = mysql_query("SELECT * FROM table1 WHERE Email= ''", $link);
    $num_rows2 = mysql_num_rows($result2);

    $checkUsrN = false;
    $checkEmail = false;
    $mesg = "The following is bad: ";

    if($num_rows == 0)
    {
       $checkUsrN = true;         
    }
    else{
        $mesg .= "Username";
    }

    if($num_rows2 == 0)
    {
       $checkEmail = true;
    }
    else{
        $mesg .= "Email";
    }

    if($checkUsrN == false || $checkEmail == false)
    {
       echo $mesg;
       break;
    }

How is that for your purpose? 那是为了你的目的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM