我从mysql数据库中获取文本,我通过URL中的ID获取它:
site.php?id = 1等等
什么被认为是最安全的防止SQL注入和东西.这种方式是否正确:
<?php
$news_id = $_GET['news_id'];
if(!is_numeric($news_id)) die('Wrong');
//mysql_query and stuff here
?>
Run Code Online (Sandbox Code Playgroud)
或者这样:
<?php
$news_id = $_GET['news_id'];
if(!intval($news_id)) die('Wrong');
//mysql_query and stuff here
?>
Run Code Online (Sandbox Code Playgroud)
为什么不使用预处理语句,这是处理sql注入攻击的正确方法.
但是,使用intval将字符串转换为整数,然后将其放入预准备语句中,您将受到保护,因为int值可能为零或负数,因此不会从查询中返回任何内容.