Log in with email or user name

Source: Internet
Author: User
I'd like to ask if I have a linked fields called e-mail and Passwrod.

$email = $_POST['email'];$password = $_POST['password'];$login     = mysql_query("SELECT * FROM `users`JOIN `users_profile` USING (id)WHERE `email` = '".$email."' AND `password` = '".$password."' OR `username` = '".$email."' AND `password` = '".$password."'

That's my sign-in language.
The user entered the email or password to login successfully:

However, if the user input username (username) and password are also eligible to log in

`username` = '".$email."' AND `password` = '".$password."'

Just the above language can only be entered into email and password login
If you enter username and password, you'll fail.
Make sure join Users_profile and use ID no problem
Where is the problem?

Reply content:

I'd like to ask if I have a linked fields called e-mail and Passwrod.

$email = $_POST['email'];$password = $_POST['password'];$login     = mysql_query("SELECT * FROM `users`JOIN `users_profile` USING (id)WHERE `email` = '".$email."' AND `password` = '".$password."' OR `username` = '".$email."' AND `password` = '".$password."'

That's my sign-in language.
The user entered the email or password to login successfully:

However, if the user input username (username) and password are also eligible to log in

`username` = '".$email."' AND `password` = '".$password."'

Just the above language can only be entered into email and password login
If you enter username and password, you'll fail.
Make sure join Users_profile and use ID no problem
Where is the problem?

Be OR careful with parentheses.

SELECT * FROM `users`JOIN `users_profile` USING (id)WHERE (`email` = '".$email."' AND `password` = '".$password."') OR (`username` = '".$email."' AND `password` = '".$password."')

However, it is not recommended to use such SQL.

You can first in the code with the regular judge of the user input is email or username, if the email is the email field query, otherwise use username field.

Reference: http://tool.oschina.net/regex#

First of all, your code is easy to inject with SQL , so please use at least mysqli and personally strongly recommend PDO.

Then, my personal idea is to assume that the front-end page of the form form has a similar input:

We use here name="login" to replace email or username , then:

$field = filter_var($_POST['login'],FILTER_VALIDATE_EMAIL) ? 'email':'username';// 这里的 $field 就是你的 欄位 (这边翻译为 字段 )

Then the SQL statement is probably like this:

$email = $_POST['email'];WHERE `".$field."` = '".$email."'

Once again, it's dangerous to say that.

Data reference: Http://php.net/manual/en/func ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.