Jump to content

Recommended Posts

The code I am using designed to display the terms I am using in my search.

 

For example:

 

.php?description=red&purple&widgets displays red and purple widgets.

 

However, I am also echoing the terms so people know what they are searching for:

 

"Your are searching for red and purple widgets"

 

However, by using the & sign it now displays "Your are searching for red"

 

If I using .php?description=red%purple%widgets then nothing is displayed.

 

 

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

Link to comment
https://forums.phpfreaks.com/topic/229535-sanitised-code-stopping-at/
Share on other sites

By using stripslashes() after mysql_real_escape_string(), you're effectively un-sanitizing the string you just sanitized. You also don't need to use htmlentities() to insert data into a database. It would be used when displaying the data.

 

EDIT: You shouldn't use stripslashes() at all without first checking whether get_magic_quotes_gpc() is TRUE.

There's nothing you can do to stop people from entering whatever they want in the url string. You have to validate that the data received is at least of the type expected, and sanitize it accordingly.

 

Can you post that code in context with how it's actually being used, and also how you're encoding the values for the url string?

Hi,

 

This is the code.  Am I worrying to much than someone can inject code?

 

<?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 LIMIT 0, 15";
}


$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>.';
}

if( !$result = mysql_query($query) ) {
     echo "<br>Query string: $query<br>Produced error: " . mysql_error() . '<br>';
}

?>

<?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;


}
?> 

No you're not worrying too much :)  Each variable that comes from $_GET needs to have mysql_real_escape_string() applied to it once and only once.  Then you can safely insert it into your query (inside quotes, as you are doing already).

 

If you don't do that, then your users can gain full control of your database.

 

htmlentities() is used when you want to display data back to the user.  It's not needed before doing an SQL query.  Usually it's used on data you have just fetched from the database.

 

For example:

 

function sanitizeString($string)
{
    return mysql_real_escape_string($string);
}

$description = sanitizeString($_GET['description']);
$query .= " WHERE description like '%$description%'";

 

This is safe.

 

You also need to learn what magic_quotes_gpc is and whether or not it's enabled on your server.

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.