Current location: Hot Scripts Forums » Programming Languages » PHP » strip_tags or htmlspecialchars


strip_tags or htmlspecialchars

Reply
  #1 (permalink)  
Old 06-15-08, 12:59 PM
naissa naissa is offline
Wannabe Coder
 
Join Date: Jul 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
strip_tags or htmlspecialchars

I'm deciding whether to use strip_tags or htmlspecialtags when processing input into the mysql database.

What is the upsides/downsides of using each? I want people to enter text without html/php tags.

Also when should we use stripslashes? Before entering input into a database or after retrieving input from a database?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 06-15-08, 06:05 PM
Kelvin Kelvin is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Use strip_tags, it actually removes the tags from the string.

Htmlspecialchars converts charactors used in html (such as <> etc) into their text equivilant, i.e. < gets converted to "&lt;" and > to "&gt;".

Run this code, it'll show you the effects:

PHP Code:

<?php


$test_string 
"<br>So Here Are...<br>";

echo 
"You should see no tags: " strip_tags($test_string);
echo 
"<br>";  
echo 
"You should see the tags represented as code: " htmlentities($test_string);
?>
Reply With Quote
  #3 (permalink)  
Old 06-16-08, 03:59 PM
naissa naissa is offline
Wannabe Coder
 
Join Date: Jul 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!

Also,

Also when should we use stripslashes? Before entering input into a database or after retrieving input from a database?
Reply With Quote
  #4 (permalink)  
Old 06-16-08, 04:29 PM
Keyne's Avatar
Keyne Keyne is offline
Newbie Coder
 
Join Date: May 2007
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Before entering input, I think. But in some cases you'll need after.
Reply With Quote
  #5 (permalink)  
Old 06-16-08, 05:26 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
If you have magic quotes gpc enabled, PHP will automatically escape all quotes by putting a backslash in front of them. To remove them you can use stripslashes(). It's however rarely used nowadays...

www.php.net/stripslashes
Reply With Quote
  #6 (permalink)  
Old 06-18-08, 03:15 AM
heath heath is offline
New Member
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
What is rarely used nowadays... stripslashes?
Reply With Quote
  #7 (permalink)  
Old 07-05-08, 12:49 PM
naissa naissa is offline
Wannabe Coder
 
Join Date: Jul 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
stripslashes is rarely used nowadays. Question - What functions should one use for input? To make sure input safe.
Reply With Quote
  #8 (permalink)  
Old 07-05-08, 06:23 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
That depends on what you want to do with it.
Reply With Quote
  #9 (permalink)  
Old 07-06-08, 03:03 AM
dustin56 dustin56 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Using either addslashes() or having magic_quotes_gpc on, in combination with strip_tags() *should* make the data safe to insert into a database. But really, you should be using regular expressions to check the data submitted by forms to ensure that it
1.) does not contain malicious code
2.) matches the data type of the database field into which it will be inserted
3.) meets any other specific requirements of the application

You can always checkout PEAR. There are some pretty decent form handlers available that could save you some time.
Reply With Quote
  #10 (permalink)  
Old 07-10-08, 09:55 AM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dustin56 View Post
Using either addslashes() or having magic_quotes_gpc on, in combination with strip_tags() *should* make the data safe to insert into a database. But really, you should be using regular expressions to check the data submitted by forms to ensure that it
1.) does not contain malicious code
2.) matches the data type of the database field into which it will be inserted
3.) meets any other specific requirements of the application

You can always checkout PEAR. There are some pretty decent form handlers available that could save you some time.
Does any of that prevent Cross Site Scripting type injections? NO. The best thing to do is use a regex to only allow chars that are acceptable.
__________________
Whatever you decide, you should make sure best security methods are used and practiced. Should you really need more help, PM me.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help writing htmlspecialchars code correctly moroose PHP 2 04-25-08 05:12 AM
Whats wrong with this? Bantam PHP 11 09-24-04 05:15 AM


All times are GMT -5. The time now is 07:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.