Hi there guys,
In the code review section of this forum, zabmilenko is offered some suggestions to make my script more secure and be better formed, but I'm having problems finding enough out about some of the items in question to integrate them properly. My first hurdle is mysql_real_escape_string.
zabmilenko states
Instead of using addslashes() for your db input protection, you should use mysql_real_escape_string(). It works with your database server encoding method, which addslashes cannot do.
and
You appear to use addslashes on your db input, then use the inverse stripslashes on the output. In order for this to make sense, you would have to addslashes twice per field. The first time you use addslashes, it would escape the string for the query. The end result of your overuse of stripslashes is parts of the text disappearing if any backslashes are used.
So, I looked it up.
Escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used.
Now, I use stripslashes and addslashes because if I don't, then my text is messed up. I looked in some other scripts I've used in the past, and found that in their database insertion code.
Here's my current dbinsert block:
$author = addslashes ($row['author']);
$author_email = addslashes ($row['author_email']);
$authorweb = addslashes ($row['authorweb']);
$authorwebname = addslashes ($row['authorwebname']);
$title = addslashes ($row['title']);
$testtext = addslashes ($row['testtext']);
$highlight = addslashes ($row['highlight']);
and my retrieval:
$id = $row['id'];
$date = $row['date'];
$author = stripslashes ($row['author']);
$author_email = stripslashes ($row['author_email']);
$authorweb = stripslashes ($row['authorweb']);
$authorwebname = stripslashes ($row['authorwebname']);
$title = stripslashes ($row['title']);
$testtext = stripslashes ($row['testtext']);
$highlight = stripslashes ($row['highlight']);
Should it simply be:
dbinsert:
$author = mysql_real_escape_string ($row['author']);
$author_email = mysql_real_escape_string ($row['author_email']);
$authorweb = mysql_real_escape_string ($row['authorweb']);
$authorwebname = mysql_real_escape_string ($row['authorwebname']);
$title = mysql_real_escape_string ($row['title']);
$testtext = mysql_real_escape_string ($row['testtext']);
$highlight = mysql_real_escape_string ($row['highlight']);
retrieval:
$author = $row['author'];
$author_email = $row['author_email'];
$authorweb = $row['authorweb'];
$authorwebname = $row['authorwebname'];
$title = $row['title'];
$testtext = $row['testtext'];
$highlight = $row['highlight'];
Or does the fact that magic quotes can mess this whole thing up require me to use something like what was on the php.net site?
<?php
// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not a number or a numeric string
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
OR die(mysql_error());
// Make a safe query
$query = sprintf("SELECT * FROM users WHERE user=%s AND password=%s",
quote_smart($_POST['username']),
quote_smart($_POST['password']));
mysql_query($query);
?>
What concerns me is that the script is available for public download, so although I may have magic_quotes set to where this will work, the next guy in line might not. The function from the php.net site confuses me more than it helps, because I need the slashes added & stripped, regardless of whether I'm dealing with a number or not. Else, I\'ll have text that doesn\'t look really hot.
thanks,
json