Jump to content

Securing $_post


AdamHull12

Recommended Posts

Hello, 

I am quite new to the php and website scene and i am trying to find the best way to validate and sterilize my $_post the way i have come up with is 

$id = filter_var(mysql_real_escape_string($_POST['id']),FILTER_SANITIZE_NUMBER_INT);

or

 

$id = mysql_real_escape_string($_POST['id']);
$id1 = filter_var($id,FILTER_SANITIZE_NUMBER_INT);

which will be the best way to do it or is there a better way.

 

Thanks 

Edited by AdamHull12
Link to comment
Share on other sites

The first step is to stop using nonsense terms like “sterilizing”.

 

Data is not “dirty”, so it cannot be “sterilized”. By itself, data doesn't do anything. The question is what you do with it. Do you want to insert the data into an SQL query? An HTML document? A JavaScript context? A PDF? Each case requires an entirely different security strategy.

 

So any attempt of coming up with some magical universal “filter” is futile and conceptually wrong. You need to choose an appropriate solution for the specific context. For SQL queries, you either use prepared statements or manually SQL-escape the data. For an HTML context, you need HTML-escaping. To pass data to JavaScript, you use Ajax. Like I said, there is no one-size-fits all solution.

Link to comment
Share on other sites

If you just want to check to see if it is a valid integer :

<?php
/* If you just want to check to see if it's a valid integer */
if (isset($_POST['id']) &&  !filter_var($_POST['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
   echo "I'm not an integer<br>";
} elseif (isset($_POST['id'])) {
   echo 'The id is ' . $_POST['id'] . '<br>';
}

?>
<form action="" method="post">
Enter Number <input type="text" name="id" >
<input type="submit" name="submit" value="Submit">
</form>

An maybe this will clear up the confusion : http://us.php.net/manual/en/intro.filter.php

$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
Edited by Strider64
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.