/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Question about strip_tags

Hello all, quick question hopefully.

I’m inserting data into MySQL, one of the rows is the “description” row, in my insert file it looks like this:

[code=php]$description = mysql_real_escape_string(strip_tags($_POST[‘description’], ‘<p><i><ul><ol><li><b>’));[/code]

I am able to insert html tags, and as you all can see, it is supposed to only allow those tags listed in the variable, its my first time using strip_tags so I got curious and I inserted an h1 tag and it did pass it to the DB, isn’t this script supposed to block that h1 tag or any other tag not listed in there for that matter?

Thanks in advance for any help provided ?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 26.2011 — Seems to work OK for me:
[code=php]
<?php
$test = <<<EOD
<h1>H1 Tag</h1>
<p>This is a test. It is <i>only</i> a test.</p>
<div>This here's the end.</div>
EOD;

mysql_connect('localhost', '####', '####');
$description = mysql_real_escape_string(strip_tags($test, '<p><i><ul><ol><li><b>'));
echo "<pre>".htmlspecialchars($description)."</pre>";
[/code]

Output:
<i>
</i>H1 Tagn&lt;p&gt;This is a test. It is &lt;i&gt;only&lt;/i&gt; a test.&lt;/p&gt;nThis here's the end.
Copy linkTweet thisAlerts:
@Sub_SevenauthorApr 26.2011 — Hey NogDog,

While trying to find out why this is not working for me I found that this does not work:
[code=php]<?php
$test = '<h1>H1 Tag</h1><p>and a p tag</p>';
$description = mysql_real_escape_string(strip_tags($test, '<p><i><ul><ol><li><b>'));
echo $description;
?>[/code]

(I made it simpler since I was removing unnecessary stuff to narrow down the problem)

And this works ok:
[code=php]<?php
$test = '<h1>H1 Tag</h1><p>and a p tag</p>';
$description = strip_tags($test, '<p><i><ul><ol><li><b>');
echo $description;
?>[/code]


From my end, it seems that mysql_real_escape_string is disabling strip_tags, is that supposed to happen? I don't think so...

Thanks.
Copy linkTweet thisAlerts:
@NogDogApr 26.2011 — Hey NogDog,

While trying to find out why this is not working for me I found that this does not work:
[code=php]<?php
$test = '<h1>H1 Tag</h1><p>and a p tag</p>';
$description = mysql_real_escape_string(strip_tags($test, '<p><i><ul><ol><li><b>'));
echo $description;
?>[/code]
...[/QUOTE]


Works for me ? (PHP 5.2.4 on Windows 7 w/Apache 2.2.11 and MySQL 5.1.36). When I do a View Source of the output page, I get:
<i>
</i>H1 Tag&lt;p&gt;and a p tag&lt;/p&gt;
Copy linkTweet thisAlerts:
@Sub_SevenauthorApr 26.2011 — I wish I was lying but I'm not,

(PHP 5.2.17 on Windows XP w/Apache mod_fcgid/2.3.5 and MySQL 5.1.52).

When using mysql_real_escape_string my source code looks like this:

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
</body>
</html>[/code]


It isn't echoing anything...
Copy linkTweet thisAlerts:
@criterion9Apr 26.2011 — I wish I was lying but I'm not,

(PHP 5.2.17 on Windows XP w/Apache mod_fcgid/2.3.5 and MySQL 5.1.52).

When using mysql_real_escape_string my source code looks like this:

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
</body>
</html>[/code]


It isn't echoing anything...[/QUOTE]

Are you connecting to the database first? mysql_real_escape_string can't work without first being connected...
Copy linkTweet thisAlerts:
@Sub_SevenauthorApr 26.2011 — Ok, now I have more questions, I wasn't connecting to a DB on that test page, maybe that's why it isn't working.

I'm going to ask my question using a different approach.

Let's forget for a minute about mysql_real_escape_string and focus on strip_tags.

Can strip_tags be used to prevent a user from inserting some HTML tags into a DB? Or can it only be used to remove those HTML tags while rendering the page on the browser?

This is my original file (without removing anything for the sake of information)
[code=php]<?php include($_SERVER['DOCUMENT_ROOT']."/includes/connect.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/adminheader.php");
mysql_set_charset('utf8');
$line_01 = mysql_real_escape_string(htmlspecialchars($_POST['cp_cat_line_01']));
$line_02 = mysql_real_escape_string(htmlspecialchars($_POST['cp_cat_line_02']));
$line_03 = mysql_real_escape_string(htmlspecialchars($_POST['cp_cat_line_03']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['cp_cat_name']));
$description = mysql_real_escape_string(strip_tags($_POST['description'], '<p><i><ul><ol><li><b>'));
$img_title = mysql_real_escape_string(htmlspecialchars($_POST['img_title']));
$brand_01 = mysql_real_escape_string(htmlspecialchars($_POST['brand_01']));
$brand_02 = mysql_real_escape_string(htmlspecialchars($_POST['brand_02']));
$brand_03 = mysql_real_escape_string(htmlspecialchars($_POST['brand_03']));
$brand_04 = mysql_real_escape_string(htmlspecialchars($_POST['brand_04']));
$brand_05 = mysql_real_escape_string(htmlspecialchars($_POST['brand_05']));
$brand_06 = mysql_real_escape_string(htmlspecialchars($_POST['brand_06']));
$brand_07 = mysql_real_escape_string(htmlspecialchars($_POST['brand_07']));
$brand_08 = mysql_real_escape_string(htmlspecialchars($_POST['brand_08']));
$brand_09 = mysql_real_escape_string(htmlspecialchars($_POST['brand_09']));
$brand_10 = mysql_real_escape_string(htmlspecialchars($_POST['brand_10']));
$sql="INSERT INTO cp_category (cp_cat_line_01, cp_cat_line_02, cp_cat_line_03, cp_cat_name, description, img_title, brand_01, brand_02, brand_03, brand_04, brand_05, brand_06, brand_07, brand_08,
brand_09, brand_10)
VALUES ('$line_01','$line_02','$line_03','$name','$description','$img_title','$brand_01','$brand_02','$brand_03','$brand_04','$brand_05','$brand_06','$brand_07','$brand_08',
'$brand_09','$brand_10')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<div id="admin">Success</div>';

mysql_close($con);
?>
<div id="clearfooter"></div>
</div><!-- admincontent div end -->
<div id="footer">
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php"); ?>
</div>
</body>
</html>[/code]


Here I am connecting to a DB.

Thanks criterion9.
Copy linkTweet thisAlerts:
@NogDogApr 26.2011 — strip_tags() doesn't know/care what you do with its return value, that's up to you. ? However, note that if you allow [i]any[/i] tags, strip_tags() does not do anything about tag attributes for any tags you allow, thus a malicious user could include stuff you don't want within the tags, e.g.:
<i>
</i>&lt;p onmouseover='window.open("http://example.com/");'&gt;Hello, World.&lt;/p&gt;
Copy linkTweet thisAlerts:
@Sub_SevenauthorApr 26.2011 — Thanks for the heads up NogDog, I am aware of the security problem when it comes to those attributes and the damage they can cause, in this particular case I'm not really concerned, this is for a single person, he will be the only one modifying this DB thus it wont be out in the wild.

Now, what about my question about strip_tags, can it be used to prevent some tags from being inserted into a DB, or it only prevents from displaying some tags on the browser, or both?

Thanks so much for the help.
Copy linkTweet thisAlerts:
@NogDogApr 27.2011 — Like I said, strip_tags() doesn't know what you are planning to do with its return value, so you can use it in any situation where you desire to make use of its functionality: removing HTML-type tags from a string.

I have no idea why you are having the problem you are reporting, and I'm unable to reproduce it. Maybe it's a character set thing? Maybe it's gremlins? If worse comes to worst you could look at something like TinyMCE for the input, using it's config options to limit what tags it will support.
Copy linkTweet thisAlerts:
@Sub_SevenauthorApr 27.2011 — I'm sorry and glad to say that I have found the problem, I have two files, one that inserts data and one that updates it, the variables were different and I didn't notice that until now, at least everything is working as it is supposed to now...

Thanks so much for the help provided :o
Copy linkTweet thisAlerts:
@NogDogApr 27.2011 — Well, I'm glad at least that you did not discover a PHP bug. ?

Don't forget to mark this thread "resolved" via the "thread tools" option near the top.
×

Success!

Help @Sub_Seven spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...