4
<?php
    echo $_GET['id'];
?>

Doesn't look very safe to me.. What is our best option to show an GET element?

Something like a preg_replace on all the special characters, or htmlspecialchars?

5 Answers5

5

Depends on what you are doing to do with $_GET['id'];

If you are looking to insert it into database , Just make use of Prepared Statements. [That suffices]

If you just want to display it on your HTML page, make use of this code.

<?php
    echo htmlentities($_GET['id']);
?>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
3
<?php
    echo htmlspecialchars($_GET['id']);
?>
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
3

htmlspecialchars() if it is a string, or cast to the appropriate type if it is numeric (intval(), or (int) etc.), for example:

$id = (int)$_GET['id'];
//or
echo (int)$_GET['id'];
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
2

If it's id, I think it should be numeric - then echo intval($_GET['id']);

u_mulder
  • 54,101
  • 5
  • 48
  • 64
1

This should be enough:

htmlspecialchars($_GET['id'], ENT_QUOTES, "UTF-8");
sybear
  • 7,837
  • 1
  • 22
  • 38