简体   繁体   中英

PHP, How to secure GET variable from URL

Here is URL:

http://ex.com/members.php?id=5320

Here is code:

mysqli_query("SELECT * from members where id='$_GET[id]'");

which method is secure?

Use prepared statements and bind variables when you're using MySQLi

$stmt = $mysqli->prepare("SELECT * from members where id=?");
$stmt->bind_param('i', $_GET['id']);
$stmt->execute();

You may also wish to validate that $_GET['id'] is an integer first, and return an error message if it isn't rather than have all the overhead of a db query to return nothing.

Read this answer to a previous question to understand why yu should take this approach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM