Hướng dẫn mysql_escape_string php8

[PHP 4 >= 4.0.3, PHP 5]

mysql_escape_stringEscapes a string for use in a mysql_query

Description

mysql_escape_string[string $unescaped_string]: string

This function is identical to mysql_real_escape_string[] except that mysql_real_escape_string[] takes a connection handler and escapes the string according to the current character set. mysql_escape_string[] does not take a connection argument and does not respect the current charset setting.

Parameters

unescaped_string

The string that is to be escaped.

Return Values

Returns the escaped string.

Examples

Example #1 mysql_escape_string[] example

nicolas

16 years ago

Note that mysql_real_escape_string doesn't prepend backslashes to \x00, \n, \r, and and \x1a as mentionned in the documentation, but actually replaces the character with a MySQL acceptable representation for queries [e.g. \n is replaced with the '\n' litteral]. [\, ', and " are escaped as documented] This doesn't change how you should use this function, but I think it's good to know.

sam at numbsafari dot com

9 years ago

No discussion of escaping is complete without telling everyone that you should basically never use external input to generate interpreted code. This goes for SQL statements, or anything you would call any sort of "eval" function on.

So, instead of using this terribly broken function, use parametric prepared statements instead.

Honestly, using user provided data to compose SQL statements should be considered professional negligence and you should be held accountable by your employer or client for not using parametric prepared statements.

What does that mean?

It means instead of building a SQL statement like this:

"INSERT INTO X [A] VALUES[".$_POST["a"]."]"

You should use mysqli's prepare[] function [//php.net/manual/en/mysqli.prepare.php] to execute a statement that looks like this:

"INSERT INTO X [A] VALUES[?]"

NB: This doesn't mean you should never generate dynamic SQL statements. What it means is that you should never use user-provided data to generate those statements. Any user-provided data should be passed through as parameters to the statement after it has been prepared.

So, for example, if you are building up a little framework and want to do an insert to a table based on the request URI, it's in your best interest to not take the $_SERVER['REQUEST_URI'] value [or any part of it] and directly concatenate that with your query. Instead,  you should parse out the portion of the $_SERVER['REQUEST_URI'] value that you want, and map that through some kind of function or associative array to a non-user provided value. If the mapping produces no value, you know that something is wrong with the user provided data.

Failing to follow this has been the cause of a number of SQL-injection problems in the Ruby On Rails framework, even though it uses parametric prepared statements. This is how GitHub was hacked at one point. So, no language is immune to this problem. That's why this is a general best practice and not something specific to PHP and why you should REALLY adopt it.

Also, you should still do some kind of validation of the data provided by users, even when using parametric prepared statements. This is because that user-provided data will often become part of some generated HTML, and you want to ensure that the user provided data isn't going to cause security problems in the browser.

rohankumar dot 1524 at gmail dot com

1 year ago

There is requirement for old projects which are using `mysql_escape_string`, and upgrading the PHP version to 7 and above. Basically this happens in maintenance projects where we don't know how many files the functions are used in application. We can use [mysqli.real-escape-string][1] for the function:

If you have a typical connection file like `conn.php`

    $conn = new mysqli[$host, $user, $password, $db];
    // may be few more lines to handle the $conn
    if [!function_exists['mysql_escape_string']] {
        function mysql_escape_string[$sting]{ // if mysql_escape_string not available
            return $conn->real_escape_string[$string]; // escape using the $conn instance
        }
    }

  [1]: //www.php.net/manual/en/mysqli.real-escape-string.php

strata_ranger at hotmail dot com

12 years ago

There's an interesting quirk in the example #2 about SQL injection:  AND takes priority over OR, so the injected query actually executes as WHERE [user='aidan' AND password=''] OR ''='', so instead of returning a database record corresponding to an arbitrary username [in this case 'aidan'], it would actually return ALL database records.  In no particular order.  So an attacker might be able to log in as any account, but not necessarily with any control over which account it is.

Of course a potential attacker could simply modify their parameters to target specific users of interest:

Chủ Đề