So, I've finished a great deal of the site I am working on, but I want to sanitize the data from the user now.
The website is an auction style website. A form exists which asks the user for data about an item they wish to auction off. When the form POSTs, the data is entered into variables in php, then submitted to a database, as such:
if(isset($_POST['title'])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");//array of allowed extensions
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
/*
* Set variables for the _POST data (except the file).
*/
$title = $_POST['title'];
$description = $_POST['description'];
$type = $_POST['description'];
$startPrice = $_POST['startPrice'];
$reservePrice = $_POST['reservePrice'];
$buyPrice = $_POST['buyPrice'];
It then verifies that the file uploaded with the form is an image of a valid format and size, copies it to its permanent location, and sets the URL variable for the image.
Then it enters the database like this:
if (!$this->db->query("INSERT INTO tbl_auction_listing VALUES(NULL, '$userid', '$title', '$description', '$img_url', '16', NOW(), NOW() + INTERVAL 5 DAY, '$startPrice', '$reservePrice', '$buyPrice', '0', '1')")) {
echo '<h2> Item did not enter successfully, please try again </h2>';
//redirect(site_url() . 'user/fail/'); exit();
}
Its quite important that a user not be able to use any custom HTML, or escape the current command in php so that they can execute shell commands.
Anyway, once the item data is retrieved later from the db, it is formatted similar to this (some lines omitted, just wanting you to see how it's coded for example)
$query = $this->db->query("SELECT * FROM item{$item} WHERE winning = '1';");
foreach ($query->result() as $row)
{
$currentHighBid = $row->bid;
// if the row is null, it means there's no bids- initialize variables and set the qualifying bid to 100 satoshi.
if ($currentHighBid == NULl) {
$currentHighBid = NULL;
$currentWinner = NULL;
$winnerMaxBid = NULL;
$ip = NULL;
$winning = NULL;
$qualifyingbid = $startprice;
$displayQualifyingBid = number_format($qualifyingbid, 8, '.', ''); // format to decimal places rather than scientific notation
}
else {
$currentHighBid = $row->bid;
$currentWinner = $row->user_pk;
$winnerMaxBid = $row->maxbid;
$ip = $row->ip;
$winning = $row->winning;
$qualifyingbid = $currentHighBid * 1.01; // the next allowed bid should be 1% higher than the current bid
$displayQualifyingBid = number_format($qualifyingbid, 8, '.', ''); // format to decimal places rather than scientific notation
}
if ($currentWinner == $userid && $open == 0) //fix for undefined
echo " <h2 class='notice'> You won! Congratulations!</h2>";
//enter the pay form here
if ($currentWinner == $userid && $open == 1) //fix for undefined
echo " <h2 class='notice'> You are currently the high bidder.</h2>";
//enter the pay form here
if ($currentHighBid == NULL) //fix for undefined
echo " <h2 class='notice'> The item currently has no bids</h2>";
else
echo " <h2 class='price'> The current bid is $currentHighBid BTC <a href='/listings/bids/$item'>(Click for Bid History)</a></h2>";
if ($open == 1)
echo " <h2 class='price'> The next acceptable bid is $displayQualifyingBid BTC</h2>";
if ($remaining > 0)
echo "<h2 class='notice'> This item has $remaining remaining.</h2>";
}
How do I make sure these are escaped\sanitized properly?