Jump to content

Is This How Sanitising Code Works?


justlukeyou

Recommended Posts

Hi,

 

I'm having a first attempt at sanitising code, but I'm not actually sure what I'm doing and how I know if it works.

 

This is the code I have inserted, if I enter "description=re#d%widget" the description query ends so it displays everything 'red'.  Not just everything 'red widget'.

 

 

$description = mysql_real_escape_string($description);
$description = stripslashes($description);
$description = htmlentities($description);
return $var;

$price = mysql_real_escape_string($price);
$price = stripslashes($price);
$price = htmlentities($price);
return $var;

 

 

 

 

<?php

ini_set('display_errors', 1);
error_reporting(-1);



$query = "SELECT * FROM productfeed";

if(isset($_GET['description']) && !empty($_GET['description']))
{
$description = $_GET['description'];
$query .= " WHERE description like '%$description%'";
}

if(isset($_GET['price']) && !empty($_GET['price']))
{
$price = explode('-', $_GET['price']);
$lowPrice = (int)$price[0];
$highPrice = (int)$price[1];

$query .= " AND price BETWEEN $lowPrice AND $highPrice";
}


$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))

{

$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

echo "<div class='productdisplayshell'>
<div class='productdisplayoutline'>
<div class='productborder'><center>
<a href='$link' target='_blank'><img src='$image' width=\"95%\" /></a>
</center> </div></div>
<div class='productdescriptionoutline'>
<div class='productdescriptionbox'>
<a href='$link' target='_blank' >$description</a>
</div>
<div class='productfulldescriptionbox'>$fulldescription</div>
</div>
<div class='productpriceoutline'>
<div class='productpricebox'>
<center>&#163; $price</center>
</div>
<div class='productbuybutton'>
<center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center>
</div>
</div>
</div>";
} 

if ($_GET['description'] == $description ) {
echo 'Sorry, this product is not available.  Please visit our <a href="http://www.domain.co.uk">Homepage</a>.';
}


?>

<?php
function sanitizeString($description)
{
$description = mysql_real_escape_string($description);
$description = stripslashes($description);
$description = htmlentities($description);
return $var;

$price = mysql_real_escape_string($price);
$price = stripslashes($price);
$price = htmlentities($price);
return $var;


}
?> 

Link to comment
Share on other sites

NO, running strip slashes after mysql_real_escape_string() will defeat the purpose of the function.

htmlentities is for displaying user data to a page.

 

I test for expected data type, strip out anything not allowed, then pass it through mysql_real_escape_string.

 

For instance, if I expect the user to pass a number.

$number = (isset($_POST['number'])) ? intval(strip_tags($_POST['number'])) : 0;

$sql = sprintf("SELECT * FROM table WHERE id = %d",mysql_real_escape_string($number));

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.