Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-25-2012, 02:14 AM   PM User | #1
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
How to escape the results of a comment form

How would you escape characters like single ('') and double ("") quotes, back/forward slashes (/\),etc in the results of a comment form?
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps
elitis is offline   Reply With Quote
Old 02-25-2012, 03:51 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
If you are inserting in to a MySQL database you can use mysql_real_escape_string().
Inigoesdr is offline   Reply With Quote
Old 02-25-2012, 06:52 AM   PM User | #3
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Quote:
Originally Posted by Inigoesdr View Post
If you are inserting in to a MySQL database you can use mysql_real_escape_string().
I have used it. It adds the forward slashes but echoes them along with the comments. Maybe what I asked isn't exactly what I meant. I want to echo the comments as normal text. I don't want the backslashes echoed as well.
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps
elitis is offline   Reply With Quote
Old 02-27-2012, 10:28 PM   PM User | #4
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Issue resolved. Lol, I randomly remembered stripslashes(). Never used it before now but once I thought about my "problem", the name stripslashes just kinda made sense.
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps
elitis is offline   Reply With Quote
Old 02-27-2012, 10:37 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No no, something else is wrong. You shouldn't have to stripslashes.
I would presume you have magic_quotes_gpc enabled. This directive escapes the quotes for you, but it isn't sufficient for database insertions, nor do either magic_quotes or real_escape_string familiar with each other. You need to use stripslashes BEFORE you insert:
PHP Code:
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    
$input stripslashes($input);
}
$input mysql_real_escape_string($input); 
For example. This only needs to be done on data provided through gpc, so anything from a user.

Edit:
Aaahhh sorry I jumped to conclusions which I can't substantiate. I assumed you are executing stripslashes upon drawing the data from your query. As you can see I executed stripslashes before the insertion (which is what you should do). You don't want to execute stripslashes upon query since if I planned to insert with \\ and magic_quotes_gpc disappears (and it will, its a matter of when not if), then when you stripslash say \\computer\path, it will end up being \computer\path, which is not the intention. Stripslash before insertion, not during selection.

Last edited by Fou-Lu; 02-27-2012 at 10:39 PM..
Fou-Lu is offline   Reply With Quote
Old 02-27-2012, 11:12 PM   PM User | #6
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
No no, something else is wrong. You shouldn't have to stripslashes.
I would presume you have magic_quotes_gpc enabled. This directive escapes the quotes for you, but it isn't sufficient for database insertions, nor do either magic_quotes or real_escape_string familiar with each other. You need to use stripslashes BEFORE you insert:
PHP Code:
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    
$input stripslashes($input);
}
$input mysql_real_escape_string($input); 
For example. This only needs to be done on data provided through gpc, so anything from a user.

Edit:
Aaahhh sorry I jumped to conclusions which I can't substantiate. I assumed you are executing stripslashes upon drawing the data from your query. As you can see I executed stripslashes before the insertion (which is what you should do). You don't want to execute stripslashes upon query since if I planned to insert with \\ and magic_quotes_gpc disappears (and it will, its a matter of when not if), then when you stripslash say \\computer\path, it will end up being \computer\path, which is not the intention. Stripslash before insertion, not during selection.
so, stripslashes() is meant to strip slashes before a data insertion, not after if I understand you correct.
If this is correct (and I don't doubt you are), then why does it say,"This function can be used to clean up data retrieved from a database or from an HTML form" on w3schools.com?

Quote:
You don't want to execute stripslashes upon query since if I planned to insert with \\ and magic_quotes_gpc disappears (and it will, its a matter of when not if), then when you stripslash say \\computer\path, it will end up being \computer\path, which is not the intention.
This is for a comment box. Users will not be posting anything that has to do with links (and by links I assume \\computer\path could be used as one, if not then not sure of a word for this), including myself as I assume it could be a security flaw
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps

Last edited by elitis; 02-27-2012 at 11:18 PM..
elitis is offline   Reply With Quote
Old 02-28-2012, 01:18 AM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Stripslashes was designed for just this. It doesn't matter whether its used before or after when it comes to function usage.

W3schools hasn't a clue what they are doing if they suggest you stripslash retrieved data. They are not thinking ahead to the obvious issue with code that no longer has magic_quotes_gpc enabled. Stripslashes shouldn't be done if the intent isn't to strip the slashes, and with gpc disappearing very soon, querying from the database will result in unintended stripslashes.

This is just a comment box too don't forget. If the comments are for something like programming, you'll see a lot of things that shouldn't be stripslashed such as "Why does this name show as O\'Neil?".
Fou-Lu is offline   Reply With Quote
Old 02-28-2012, 01:36 AM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You shouldn't need to escape quotes in data being passed into a database at all - you just need to use PDO or mysqli prepare and bind statements that keep the data separate from the query so that you don't need to escape the data to avoid it being confused with the SQL in the query.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-28-2012, 04:30 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by felgall View Post
You shouldn't need to escape quotes in data being passed into a database at all - you just need to use PDO or mysqli prepare and bind statements that keep the data separate from the query so that you don't need to escape the data to avoid it being confused with the SQL in the query.
Correct, and this is my recommendation if you have the option available to you. Unfortunately, PDO and MySQLi are both newer PHP technologies as well as requiring certain version's of the DB's in use.
Stipslashes will still have to happen until they get rid of GPC if its enabled. And to expand just slightly, its not that you shouldn't need to escape, its that you cannot escape as they will carry as a part of the data (which would result in the identical behaviour the OP is having right now).
Fou-Lu is offline   Reply With Quote
Old 02-29-2012, 12:58 AM   PM User | #10
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
This is just a comment box too don't forget. If the comments are for something like programming, you'll see a lot of things that shouldn't be stripslashed such as "Why does this name show as O\'Neil?".
The comment box is for a review type website, where users will comment on reviews, news, etc so nothing like programming.
Here's the code I have. What specifically do I need to replace stripslashes with?

PHP Code:
<?php 
    
//Reviews start
    
$result mysql_query("SELECT * FROM `reviews` ORDER BY `date` DESC LIMIT 10");
    while(
$row mysql_fetch_array($result))
    { 
    if (
$_GET['id'] == $row['id'])
    {
    
$date $row['date'];
    echo 
stripslashes($row[title]); 
    echo 
"<br />";
    echo 
stripslashes($row[review]);
    echo 
"<br />";
    echo 
date("D, F jS"$date);
    echo 
"<h1 class='centered'>Comments</h1>";
    }
    else 
    {
    echo 
"<div class='box3'>";
    echo 
stripslashes($row[title]);
    echo 
"<br />";
    echo 
stripslashes($row[description]);
    echo 
"<br />";
    echo 
date("D, F jS"$date);
    echo 
"<a class='special' href=\"/reviews?id={$row['id']}\"> Read More...</a>";
    }
    
//Reviews end
    
    //Comments start
    
$result mysql_query("SELECT * FROM `comments` WHERE `category` = 'reviews' AND `subcategory` = '$row[id]' ORDER BY `date` DESC LIMIT 20");
    while(
$comments mysql_fetch_array($result))
    {
    if (
$_GET['id'] == $row['id'])
    {
    echo 
"<br />";
    echo 
"<div class='box'>";
    if (
$comments['reply_to'] > 0)
  {
  
$get_comment mysql_query("SELECT * FROM `comments` WHERE `id` = $comments[reply_to]");
  while(
$r_comment mysql_fetch_array($get_comment))
  {
  echo 
"<i>Originally posted by " .stripslashes($r_comment[username]). '</i>';
  echo 
"<br />";
  echo 
"<i> " .stripslashes($r_comment[comment]). '</i>';
  echo 
"<br />";
  echo 
"<i> " .Agotime($r_comment[date]). '</i>';
  echo 
"<hr />";
  }
  }
    echo 
stripslashes($comments[username]);
    echo 
"<br />";
    echo 
stripslashes($comments[comment]);
    echo 
"<br />";
    echo 
Agotime($comments['date']);
    echo 
"<a class='special' href=\"/reviews?id={$row['id']}&reply={$comments['id']}\"> Reply</a>";
    
//Replies start
    
if ($_GET['reply'] == $comments['id']) 
    {
    echo 
"
    <form action='/reviews/' method='POST'>
    <input class='field' type='text' name='name' value='Name' required='required' onFocus=\"clearText(this)\" />
    <br />
    <textarea class='field' name='comment' rows='2' cols='55' required='required'></textarea>
    <input type='hidden' name='submitted' value='1' />
    <input type='hidden' name='reply' value=\"{$comments['id']}\" />
    <input class='specialbutton' type='submit' name='submit' value='Post Comment' />
    </form>"
;
    }
    echo 
"<br />
    </div>"
;
    
//Replies end
    
}
    }
    if (
$_GET['id'] == $row['id'])
    {
    echo 
"
    <br />
    <div class='box'>
    <form action='/reviews/' method='POST'>
    <input class='field' type='text' name='name' value='Name' required='required' onFocus=\"clearText(this)\" />
    <br />
    <textarea class='field' name='comment' rows='2' cols='55' required='required'></textarea>
    <input class='specialbutton' type='submit' name='submit' value='Post Comment' />
    <input type='hidden' name='submitted' value='1' />
    </form>
    </div>"
;
    }
    
//Comments end
    
$username mysql_real_escape_string($_POST['name']);
$comment mysql_real_escape_string($_POST['comment']);
$category "reviews";
$subcategory $row['id'];
$reply mysql_real_escape_string($_POST['reply']);

if (
$_POST['submitted'] == 1)
{
if (
$username == ""
{
echo 
"Name is a required field";
exit();
}
if (
$comment == "")
{
echo 
"Comment is a required field";
exit();
}

$sql="INSERT INTO `comments` (username, comment, date, category, subcategory, reply_to)
VALUES
('$username','$comment', NOW(), '$category', '$subcategory', '$reply')"
;

if (!
mysql_query($sql))
  {
  die(
'Error: ' mysql_error());
  }
}
}
?>
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps
elitis is offline   Reply With Quote
Old 02-29-2012, 01:09 AM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Fou-Lu View Post
C And to expand just slightly, its not that you shouldn't need to escape, its that you cannot escape as they will carry as a part of the data
The purpose in escaping data when you output it is to avoid having it confused as part of something else - in this case SQL.

With the SQL in the prepare statement and the data in the bind statement there is no possibility of the one being confused for the other and so no need to escape. It is not necessary and therefore there are no escape characters.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-29-2012, 01:18 AM   PM User | #12
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Ok, so if I wanted to use Mysqli or PDO which version would I need? I currently have Mysql 5.0.90 and PHP 5.2.13.
Edit: Nevermind...
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps

Last edited by elitis; 02-29-2012 at 01:32 AM..
elitis is offline   Reply With Quote
Old 02-29-2012, 02:42 AM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by felgall View Post
The purpose in escaping data when you output it is to avoid having it confused as part of something else - in this case SQL.

With the SQL in the prepare statement and the data in the bind statement there is no possibility of the one being confused for the other and so no need to escape. It is not necessary and therefore there are no escape characters.
You misunderstand. I was simply expanding on the wording you used; shouldn't isn't correct, it should be cannot (as in you can do it, but don't). Bound values that have been escaped will record as escaped, which will just repeat the vicious cycle as it is not needed to perform any type of escape. If you bind, do not escape (which is what I'm quite sure you were implying, but the wording was open for ambiguity).

Quote:
Originally Posted by elitis View Post
Ok, so if I wanted to use Mysqli or PDO which version would I need? I currently have Mysql 5.0.90 and PHP 5.2.13.
Edit: Nevermind...
That version is sufficient so long as either PDO or MySQLi has been enabled. You can check the status of both in phpinfo(), or by using something like class_exists.
Fou-Lu is offline   Reply With Quote
Old 02-29-2012, 09:04 PM   PM User | #14
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
You misunderstand. I was simply expanding on the wording you used; shouldn't isn't correct, it should be cannot (as in you can do it, but don't). Bound values that have been escaped will record as escaped, which will just repeat the vicious cycle as it is not needed to perform any type of escape. If you bind, do not escape (which is what I'm quite sure you were implying, but the wording was open for ambiguity).



That version is sufficient so long as either PDO or MySQLi has been enabled. You can check the status of both in phpinfo(), or by using something like class_exists.
Both PDO and MySQLi have been enabled.
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps
elitis is offline   Reply With Quote
Old 03-01-2012, 03:23 AM   PM User | #15
elitis
Regular Coder

 
Join Date: Sep 2010
Posts: 319
Thanks: 9
Thanked 6 Times in 6 Posts
elitis is an unknown quantity at this point
Updated code: Is this right?

PHP Code:
 <?php 
    
//Reviews start
    
$result mysql_query("SELECT * FROM `reviews` ORDER BY `date` DESC LIMIT 10");
    while(
$row mysql_fetch_array($result))
    { 
    if (
$_GET['id'] == $row['id'])
    {
    
$date $row['date'];
    echo 
$row['title']; 
    echo 
"<br />";
    echo 
$row['review'];
    echo 
"<br />";
    echo 
date("D, F jS"$date);
    echo 
"<h1 class='centered'>Comments</h1>";
    }
    else 
    {
    echo 
"<div class='box3'>";
    echo 
$row['title'];
    echo 
"<br />";
    echo 
$row['description'];
    echo 
"<br />";
    echo 
date("D, F jS"$date);
    echo 
"<a class='special' href=\"/reviews?id={$row['id']}\"> Read More...</a>";
    }
    
//Reviews end
    
    //Comments start
    
$result mysql_query("SELECT * FROM `comments` WHERE `category` = 'reviews' AND `subcategory` = '$row[id]' ORDER BY `date` DESC LIMIT 20");
    while(
$comments mysql_fetch_array($result))
    {
    if (
$_GET['id'] == $row['id'])
    {
    echo 
"<br />";
    echo 
"<div class='box'>";
    if (
$comments['reply_to'] > 0)
  {
  
$get_comment mysql_query("SELECT * FROM `comments` WHERE `id` = $comments[reply_to]");
  while(
$r_comment mysql_fetch_array($get_comment))
  {
  echo 
"<i><p>Originally posted by " .$r_comment[username]. '</p></i>';
  echo 
"<i><p> " .$r_comment[comment]. '</p></i>';
  echo 
"<i><p> " .Agotime($r_comment[date]). '</p></i>';
  echo 
"<hr />";
  }
  }
    echo 
"<p>" .$comments[username]. '</p>';
    echo 
"<p>" .$comments[comment]. '</p>';
    echo 
"<p>" .Agotime($comments[date]). '</p>';
    echo 
"<a class='special' href=\"/reviews?id={$row['id']}&reply={$comments['id']}\"> Reply</a>";
    
//Replies start
    
if ($_GET['reply'] == $comments['id']) 
    {
    echo 
"
    <form action='/reviews/' method='POST'>
    <input class='field' type='text' name='name' value='Name' required='required' onFocus=\"clearText(this)\" />
    <br />
    <textarea class='field' name='comment' rows='2' cols='55' required='required'></textarea>
    <input type='hidden' name='submitted' value='1' />
    <input type='hidden' name='reply' value=\"{$comments['id']}\" />
    <input class='specialbutton' type='submit' name='submit' value='Post Comment' />
    </form>"
;
    }
    echo 
"<br />
    </div>"
;
    
//Replies end
    
}
    }
    if (
$_GET['id'] == $row['id'])
    {
    echo 
"
    <br />
    <div class='box'>
    <form action='/reviews/' method='POST'>
    <input class='field' type='text' name='name' value='Name' required='required' onFocus=\"clearText(this)\" />
    <br />
    <textarea class='field' name='comment' rows='2' cols='55' required='required'></textarea>
    <input class='specialbutton' type='submit' name='submit' value='Post Comment' />
    <input type='hidden' name='submitted' value='1' />
    </form>
    </div>"
;
    }
    
//Comments end
    
[B]$username stripslashes(mysql_real_escape_string($_POST['name']));
$comment stripslashes(mysql_real_escape_string($_POST['comment']));
$category 'reviews';
$subcategory $row['id'];
$reply stripslashes(mysql_real_escape_string($_POST['reply']));[/B]

if (
$_POST['submitted'] == 1)
{
if (
$username == ""
{
echo 
"Name is a required field";
exit();
}
if (
$comment == "")
{
echo 
"Comment is a required field";
exit();
}

$sql="INSERT INTO `comments` (username, comment, date, category, subcategory, reply_to)
VALUES
('$username','$comment', NOW(), '$category', '$subcategory', '$reply')"
;

if (!
mysql_query($sql))
  {
  die(
'Error: ' mysql_error());
  }
}
}
?>
__________________
Coding is a challenge, get used to it
Always remember to debug
Try the guess & check method
Break it down into simple steps
elitis is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:52 AM.


Advertisement
Log in to turn off these ads.