In the normal development process, there is the problem of SQL injection in the code, presumably we all know and understand, but also understand the common solution.
But recently there's another way to prevent SQL injection:
$sql = sprintf("SELECT * FROM `test1` WHERE `name` = unhex('%s')", bin2hex($name));
That is, using PHP's own function bin2hex
function to process the input to query string into a 16 string, and then the SQL execution process using the database itself to unhex
reverse the 16 binary string to the original normal string.
This way, even if the value of $name is 'maratrix' or 1 = 1
normally treated as a query string, there is no problem with SQL injection.
Looked up the relevant information, also did not understand that this can bypass the principle of SQL injection, I hope you help explain the reasons.
Thank you ~
Reply content:
In the normal development process, there is the problem of SQL injection in the code, presumably we all know and understand, but also understand the common solution.
But recently there's another way to prevent SQL injection:
$sql = sprintf("SELECT * FROM `test1` WHERE `name` = unhex('%s')", bin2hex($name));
That is, using PHP's own function bin2hex
function to process the input to query string into a 16 string, and then the SQL execution process using the database itself to unhex
reverse the 16 binary string to the original normal string.
This way, even if the value of $name is 'maratrix' or 1 = 1
normally treated as a query string, there is no problem with SQL injection.
Looked up the relevant information, also did not understand that this can bypass the principle of SQL injection, I hope you help explain the reasons.
Thank you ~
Bin2Hex after the $name all converted to hex format data, after stitching into SQL inside will not produce the existence of a loophole statement. It is best to suggest that you let the $name value maratrix' or 1 = 1
, and then echo the value of $sql to understand.
Because Unhex is a MySQL function, it is no problem to perform naturally in MySQL.
SELECT * from test1
WHERE name
= Unhex (' 6d6172617472697827206f722031203d2031 ');
Name no matter what value, will not split the SQL.