SQL injection _php Tutorial in PHP query login

Source: Internet
Author: User
----------------------------------------------------------------------------------------------------

For example, the following login code:

if ($l = @mysql_connect (' localhost ', ' root ', ' 123 ')) or Die (' Database connection failed ');

mysql_select_db (' Test ');

Mysql_set_charset (' UTF8 ');

$sql = ' Select ' from test where username = "$username" and password = "$password";

$res = mysql_query ($sql);

if (mysql_num_rows ($res)) {

Header (' location:./home.php ');

}else{

Die (' input error ');

}

----------------------------@chenwei Black-eyed poet --------------------------

Note that the above SQL statement, there is a great security risk, if you use the following universal password and universal user name, then you can easily access the page:

1. $sql = ' SELECT * FROM test where username = "* * *" and password = "* * *" or 1 = "1";

Obviously, the universal password for this SQL statement is: * * * "or 1 =" 1

2. $sql = ' SELECT * FROM test where username = "* * *" UNION SELECT * FROM users/* and password = "* * *";

Forward slash * indicates that the following does not execute, MySQL support Union union query, so directly query out all the data; So the universal user name for this SQL statement is: * * * "UNION SELECT * FROM users/*

However, this injection is only for SQL statements in code, if $sql = "SELECT * FROM Test where username = $username and password = $password";

The above injection is at least useless, but the method is the same;

After using PDO, SQL injection can be completely avoided, and in this fast-developing era, the framework is rampant, without much consideration for SQL injection.

----------------------------------------------------------------------------------------------------

http://www.bkjia.com/PHPjc/819111.html www.bkjia.com true http://www.bkjia.com/PHPjc/819111.html techarticle ----------------------------------------------------------------------------------------------------such as the following login code: if ($l = @mysql_connect (' localhost ', ' root ' ...

  • 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.