Is validating $_GET id in database match secure enough?

戏子无情 提交于 2019-12-06 02:44:33

It's throwing an error because mysqli_real_escape_string expects two arguments, the first of which is the connection $conn.

If you do this it should be secure enough, but it would be better to use a parameterized query. For example:

$stmt = mysqli_prepare($conn, "SELECT cols FROM posts WHERE postid = ?");
mysqli_stmt_bind_param($stmt, 'i', $id);

Checking that the id exists in the database is not secure against injection at all since you have to use a potentially malicious id in the query to do the check in the first place.

If your id column is an integer, use this:

$id = (int) $_GET['p'];

That will be safe, because the (int) type coercion strips out any illicit characters.

Making SQL safe can also be as simple as: always use parameters for dynamic values. This works for integer values and strings and dates. Then you don't need to worry about quoting or escaping or filtering. @ExplosionPills shows an example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!