Jump to content

strip_tags


garry

Recommended Posts

Just a quick question,

 

I'm currently sanitizing all user input by using the strip_tags function to remove tags. But, now that I've installed the TinyMCE editor, I want certain tags to be allowed. How can I strip all tags BUT a list that I specify? I'm sure there's a way to do it but can't find out how

 

Thanks!

Link to comment
Share on other sites

Okay can you help me improve my function to work properly please?

 

If I type: "<b>text text text</b>p>some text here</p>

 

I get the following output:

 

<b>blah blah blah</b>

<p><p>some text here</p></p>

 

Here is the function I'm using to clean the code:

 

<?php
function clean($var)
{
	if (get_magic_quotes_gpc()) {
		$var = stripslashes($var);
		}
	else {
		$var = $var;
		}

	$var = mysql_real_escape_string($var);

	$var = strip_tags($var, '<b>,<p>');

	$var = htmlentities($var);



	return $var;
}
?>

 

Can anyone help?

Link to comment
Share on other sites

Your function does some strange things for instance why are you using

 

$var = mysql_real_escape_string($var);

 

That should only be used to insert data into a database if you are just cleaning out HTML just use the following

 

$var = strip_tags($var, '<b>,<p>');

 

However your getting things like < because of the line

 

$var = htmlentities($var);

 

Also your string

 

"text text textp>some text here</p>

 

Should not return everything between the paragraph tags because its improperly formatted

Link to comment
Share on other sites

I am escaping the data because i AM adding it to the database. And I did have the the <p> formatted properly, I just quickly retyped it here and must have missed that.

 

Can you please help me fix this function?

Link to comment
Share on other sites

I'll write it out for you if your just protecting user insertion into the database then just do this

 

<?php
function clean($var)
{
	$var = mysql_real_escape_string($var);
	return $var;
}
?>

 

however if your not putting it into the database and just want to display PHP Code on a PHP page without the Code being fired but displayed instead then do this

 

 

<?php
function clean($var)
{
	$var = htmlentities($var);
	return $var;
}
?>

 

If you want to first strip out the required HTML tags and then protect the info for insertion into the database then do this

 

<?php
function clean($var)
{
	$var = strip_tags($var, '<b>,<p>');
                $var = mysql_real_escape_string($var);
	return $var;
}
?>

 

I hope that helps a bit more.

Link to comment
Share on other sites

No, my question still stands "anon".

 

And I am not asking anybody to write the code for me, I already have written my own function to clean. I'm just asking for someone to look over it and help me see what is wrong with it.

 

Your rude post wasn't a lot of help either.

Link to comment
Share on other sites

Thanks for your help minidak but this has still not fixed my problem.

 

I have already written a function to sanitize user input and to prevent sql injection as I am inserting this data into a database (hence the mysql_real_escape_string). What I'm trying to figure out is how to have all html be stripped except for the specific tags that I allow. Even after using the code I posted above, I still get this:

 

<p><b> a description goes here </b></p> <p><p> some other text goes here </p></p>

 

There is something wrong in this function, that isn't making the tags i specifically entered work:

 

function clean($var)

{

if (get_magic_quotes_gpc()) {

$var = stripslashes($var);

}

else {

$var = $var;

}

 

$AllowedTags = array("a", "b", "blink", "blockquote", "br", "caption", "center", "col", "colgroup", "comment",

                      "em", "font", "h1", "h2", "h3", "h4", "h5", "h6", "hr", "img", "li", "marquee", "ol", "p", "pre", "s",

                      "small", "span", "strike", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th",

                      "thead", "tr", "tt", "u", "ul");

 

$var = mysql_real_escape_string($var);

 

$var = strip_tags($var, '<b>,<p>');

 

$var = htmlentities($var);

 

return $var;

}

 

 

Link to comment
Share on other sites

Based upon your last post your issue is with this line

 

$var = htmlentities($var);

 

That will not make your tags work, that will disable them and just display them as standard TEXT and not HTML elements so when the page gets rendered it will look like this

<b>

and not like this BOLD

Link to comment
Share on other sites

I was advised to put that line previously for some security reason so that's why it there.

 

But even after removing this line, the problem still remains :(

 

I get this output:

 

<b>some title</b>

<p>some description</p>

 

It doesn't actually render the tags, just prints them.

Link to comment
Share on other sites

Try this it should be all you need because using $var = htmlentities($var); will make all your HTML render as text and not HTML and there is no reason why that would be a security concern unless they are inserting JavaScript into the page but thats a whole nother story

 

function clean($var)
{		
  $var = strip_tags($var, '<b>,<p>');	
$var = mysql_real_escape_string($var);

return $var;
}

 

EDIT: Also note that you will get an error if mysql_real_escape_string($var); is used before the database connection is made.

Link to comment
Share on other sites

No, my question still stands "anon".

 

And I am not asking anybody to write the code for me, I already have written my own function to clean. I'm just asking for someone to look over it and help me see what is wrong with it.

 

Your rude post wasn't a lot of help either.

 

My post, "garry", was not meant to be rude. I was merely pointing out that he had already answered your question (in regards to your comment: "Can you please help me fix this function?"). I apologize for the misunderstanding. Glad to see your problem has been solved.

 

Cheers.

Link to comment
Share on other sites

No hard feelings anon, I was just saying that I wasn't trying to ask anybody to "do it for me", I was simply asking for help to solve a question that I had so I could learn from it (I'm new to PHP).

 

But I've had to change the status to not solved as I had some more problems. Because I've had to remove the htmlentities function from my cleaning function, this means that characters such as $, & and ; will display incorrectly, am I correct?

 

And also, I needed some help with a query I was making. I am trying to use user supplied data to insert into two separate tables in one database but the second table that is inserted into needs to get information from the first table that was inserted from (the new id created) Here's an example of the code:

 

$album = clean_full($_POST['album']);
$artistid = $_POST['artist'];
$year = (int)($_POST['year']);
$review =  clean_body($_POST['review']);
$user_id = $_SESSION['user_id'];

$query = "INSERT
		  INTO albums
		  SET
		  	id = '', // This id will auto-increment in the database but I need to get the number it is assigned
			artist_id = '$artistid',
			title = '$album',
			year = '$year',
			user_id = '$user_id',
			created_at = NOW()
		 ";

$query1 = "INSERT
		  INTO reviews
		  SET
		  	id = '',
			artist_id = '$artistid',
			album_id = '?WHERE DO I GET THIS?', // I need to get the id from the previous insert and place it here
			body = '$review',
			user_id = '$user_id',
			created_at = NOW()
		 ";

$result = mysql_query($query);

 

I would really appreciate any help :)

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.