1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to strip out html code from input field?

Discussion in 'PHP' started by cgchris99, Feb 12, 2006.

  1. #1
    I have users fill out a form that gets recorded in a database. I need to be able to remove any html code from it.

    I have been looking at the strip_tags but am worried about some xss scripting issues.

    What is the best way to remove any html code from the inputed data?
    There should be no links or anything in this part of the data.

    Thanks for any advice.
     
    cgchris99, Feb 12, 2006 IP
  2. franck~

    franck~ Peon

    Messages:
    20
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I have been using this for some time now. It's easy to customize to suit your needs.

    
    <?php
    // $document should contain an HTML document.
    // This will remove HTML tags, javascript sections
    // and white space. It will also convert some
    // common HTML entities to their text equivalent.
    
    $search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                     "'<[/!]*?[^<>]*?>'si",          // Strip out HTML tags
                     "'([rn])[s]+'",                // Strip out white space
                     "'&(quot|#34);'i",                // Replace HTML entities
                     "'&(amp|#38);'i",
                     "'&(lt|#60);'i",
                     "'&(gt|#62);'i",
                     "'&(nbsp|#160);'i",
                     "'&(iexcl|#161);'i",
                     "'&(cent|#162);'i",
                     "'&(pound|#163);'i",
                     "'&(copy|#169);'i",
                     "'&#(d+);'e");                    // evaluate as php
    
    $replace = array ("",
                     "",
                     "\1",
                     "\"",
                     "&",
                     "<",
                     ">",
                     " ",
                     chr(161),
                     chr(162),
                     chr(163),
                     chr(169),
                     "chr(\1)");
    
    $text = preg_replace($search, $replace, $document);
    ?> 
    
    PHP:
    Source:
    http://www.tipsntutorials.com/tips/PHP/41
    (Sorry I don't have enough posts for live links.):)
     
    franck~, Feb 13, 2006 IP
    sarahk likes this.
  3. cgchris99

    cgchris99 Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the help. I'll give it a shot.
     
    cgchris99, Feb 13, 2006 IP
    YIAM likes this.
  4. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Thanks frank. Good work.
     
    YIAM, Feb 13, 2006 IP
  5. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #5
    Thanks franck for the excellent function.
     
    Lordo, Feb 14, 2006 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    digitalpoint, Feb 14, 2006 IP
  7. dataman

    dataman Peon

    Messages:
    94
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Why strip, just protect against it, run it through the html translation table array_flip();. This way you don't remove anything, you just make it safe!


    function to_entities ( $in )
    {
    	$temp = get_html_translation_table ( HTML_ENTITIES );
    	return ( strtr ( $in, $temp ) );
    }
    
    function to_iso ( $in )
    {
    	$temp = array_flip ( get_html_translation_table ( HTML_ENTITIES ) );
    	return ( strtr ( $in, $temp ) );
    }
    Code (markup):

    dm!
     
    dataman, Feb 14, 2006 IP
  8. Sportsplayer

    Sportsplayer Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I've got a vulnerability with xss in the advance search php code for an oscommerce store that I need fixed. I would greatly appreciate it if someone could provide the code and tell me which php I need to insert the code. Please understand I'm a DBA and not a php coder. So I need the KISS approach.
     
    Sportsplayer, Jul 20, 2006 IP
  9. aras

    aras Active Member

    Messages:
    533
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #9
    addslashes(htmlspecialchars($_POST['name']));
     
    aras, Jul 20, 2006 IP
  10. Boby

    Boby Peon

    Messages:
    207
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Here is a good PHP class:
    http://www.phpclasses.org/browse/package/2189.html

    Boby
     
    Boby, Jul 20, 2006 IP