Answer the question
In order to leave comments, you need to log in
SQL Injection Protection
Wrote a small function in PHP:
function sql_guard($method, $query, $type)
{
if ($method == 'POST')
$safe_text = ($type == 'int') ? intval($_POST["$query"]) : addslashes($_POST["$query"]);
elseif ($method == "GET")
$safe_text = ($type == 'int') ? intval($_GET["$query"]) : addslashes($_GET["$query"]);
else
$safe_text = ($type == 'int') ? intval($_REQUEST["$query"]) : addslashes($_REQUEST["$query"]);
return $safe_text;
}
$var1 = sql_guard('POST', 'input1');
$var2 = sql_guard('POST', 'input2', 'int');
Answer the question
In order to leave comments, you need to log in
I don’t agree with your line “I think what she does is not worth explaining.”
My comment may not be entirely on the topic of the question, but still I will comment on the code: the function is terrible. Despite the fact that the function has 10 lines, it takes 3 arguments at the input, there are elseif constructs, two types of quotes are used, including such things as $_GET["$query"], three ternary operators
A program written in this style causes VERY large difficulty understanding it.
besides the
line $var1 = sql_guard('POST', 'input1'); will cause an error.
PS: Read McConnell's Perfect Code
a server without pdo is a very strange thing, is such a miracle necessary?
If we replace the entry
$var2 = sql_guard('POST', 'input2', 'int');
to
$var2 = (int) sql_guard('POST', 'input2'); // this construction is shorter by one comma,
then your function can be reduced by 2 times by throwing out all the ternary conditions.
You can also replace:
$var1 = sql_guard('POST', 'input1');
on
$var1 = sql_guard($POST['input1']);
Then your function will turn into an alias and will contain only one call to addSlashes or mysql_real_escape_string
mysql_real_escape_string
intval
Only these functions need to be used, intval for numbers, but mysql_real_escape_string for all other types ... I guarantee that there will be no sql injection in this way
- All data that should not contain quotes is cut by whitelist filtering.
- Data that can be with quotes and that can be cut by filtering is processed by the mysql_real_escape_string function.
There may be encoding problems in your method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question