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
  • 66,874
  • 43
  • 94
  • 124
3
<?php
    echo htmlspecialchars($_GET['id']);
?>
Paul Draper
  • 71,663
  • 43
  • 186
  • 262
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
  • 77,023
  • 7
  • 60
  • 83
2

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

u_mulder
  • 53,091
  • 5
  • 44
  • 59
1

This should be enough:

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