Results 1 to 17 of 17

Thread: [RESOLVED] htmlentities

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Resolved [RESOLVED] htmlentities

    I tried to learn the htmlentities,

    PHP Code:
    <?php
    $str 
    "A 'quote' is <i>bold</i>";

    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str);
    echo 
    "<p>";

    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($strENT_QUOTES);
    ?>
    But all I got/output is

    Code:
    A 'quote' is <i>bold</i>
    A 'quote' is <i>bold</i>
    Iam using php5, is there anything I should enable?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: htmlentities

    using php 5.2.1 with mostly default php.ini settings, I did the following:
    PHP Code:
    <?php
      header
    ("Content-type: text/plain");
      
    $str "This is a '<strong>sentence</strong>' with some '<em>html</em>' contained \"<u>within</u>\" it";
      echo 
    'normal:' "\t\t\t" $str "\n\n";
      echo 
    'htmlentities():' "\t\t" htmlentities($strENT_QUOTES) . "\n\n";
    ?>
    and got this:
    Code:
    normal:			This is a '<strong>sentence</strong>' with some '<em>html</em>' contained "<u>within</u>" it
    
    htmlentities():		This is a '&lt;strong&gt;sentence&lt;/strong&gt;' with some '&lt;em&gt;html&lt;/em&gt;' contained &quot;&lt;u&gt;within&lt;/u&gt;&quot; it

    edit: crap, when I pasted it, it didn't show up right. run the script I posted above and you'll see. it uses the ASCII character codes ("#039;" with an ampersand before it) to replace the quotes instead of something like &quot;. this is, however, the desired affect for this function.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: htmlentities

    Kows, I got this

    vb Code:
    1. normal:         This is a '<strong>sentence</strong>' with some '<em>html</em>' contained "<u>within</u>" it
    2.  
    3. htmlentities():     This is a '&lt;strong&gt;sentence&lt;/strong&gt;' with some '&lt;em&gt;html&lt;/em&gt;' contained &quot;&lt;u&gt;within&lt;/u&gt;&quot; it

    It's terrible, not sure why.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: htmlentities

    It looks like it is working. What's the issue?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: htmlentities

    So what exactly htmlentities does? I think that's the main issue that I don't get the whole thing with those marks.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: htmlentities

    Quote Originally Posted by vbbit
    I tried to learn the htmlentities,

    PHP Code:
    <?php
    $str 
    "A 'quote' is <i>bold</i>";

    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($str);
    echo 
    "<p>";

    // Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
    echo htmlentities($strENT_QUOTES);
    ?>
    But all I got/output is

    Code:
    A 'quote' is <i>bold</i>
    A 'quote' is <i>bold</i>
    Iam using php5, is there anything I should enable?
    Right click on the page and select "View Source"
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: htmlentities

    When in doubt, consult the documentation:
    PHP: htmlentities - Manual

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: htmlentities

    HTML entities converts all HTML-ish characters into characters that a browser will parse as, well, characters, rather than actually parsing the HTML.

    it will convert a LESS THAN ("<") sign into &lt; and a GREATER THAN (">") sign into &gt;. It can replace single and double quotes, as well as some other special characters.

    what did you think it did?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: htmlentities

    Oh man, I thought what it did was to convert the tags to html. Like this <b>testing</b> , the output from that htmlentities should be testing

    I don't see any reasons to use htmlentities then, or is there?

  10. #10
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: htmlentities

    Quote Originally Posted by vbbit
    Oh man, I thought what it did was to convert the tags to html. Like this <b>testing</b> , the output from that htmlentities should be testing

    I don't see any reasons to use htmlentities then, or is there?
    You can use htmlentities to sanitize user input before you put it into a database query or display it on the web page. For example, If a malicious user enters something like:

    Code:
    HI I LIKE YOUR SITE
    <meta http-equiv="refresh" content="2;url=http://myevilhomepage.com">
    into a comment box on your webpage, and you do not modify the input before displaying it, the comment will be parsed by the webbrowser and cause it to redirect elsewhere. Wheras, if you use htmlentities() to sanitize the input, the actual code will be displayed in the browser and have no effect. -- Which is what VBForums does to HTML that you put into your post (so that you can see the html i pasted above!)
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: htmlentities

    htmlentities() is used when outputting data into an HTML document.

    Take this forum as an example. This forum lets you type any characters you like into a post. It is then the responsibility of the forum software to ensure that what you have typed does not then mess up the layout of the entire page. It does this by running htmlentities() over the post contents when it is outputted.

    As a matter of sheer principle, you should run it after retrieving data from the database, not before storing it. (Apart from, of course, situations where you're also caching the HTML in the database.)

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: htmlentities

    OK just to clarify so from what you guys talking about is that, if a user type in the input box on my webpage:

    <b>testing</b>

    Then my insert query will insert that whole <b>testing</b> to my database, and therefore, when I print out the output the text in that row to the web browser, the user will see testing, correct? But on other hand, if I used htmlentities(<b>testing</b>), then will those #$@ signs saved to my database also? and will it print out those signs to the browser also?

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: htmlentities

    Yes - but, like I say, you should sanitise the data after retrieving it from the database and before outputting it to the page, not before inserting it into the database.

  14. #14
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: htmlentities

    if you used htmlentities() and then inserted it into your database, the raw output from htmlentities() will be stored in your database ('&lt;' for '<' and '&gt;' for '>').

    however, the browser will not show the raw output to the user (unless they're viewing the source), but rather will parse the special characters as readable characters. so, "&lt;" changes to "<" and "&gt;" changes to ">", for example. if you view the source, though, you will see the raw output that the database has stored.

    make sense?

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: htmlentities

    How to sanitise the data after retrieving it?

    And now kows gets me confused. So in the database it stores '&lt;' for '<' , then in the browser, the page will displays "<" instead of '&lt;'? then the spammer is still able to redirect the page.

  16. #16
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: htmlentities

    no. if the page is HTML (rather than plain text), a browser will parse "&gt;" to show up as "<" to the end user only. if you view the source of the page (the raw output), it will still be '&gt;'.

    and uhh, you sanitize data by using htmlentities().. he meant you should do it when you're retrieving from the database, not when you're saving. so, you want to store it in the database as normal HTML, but use htmlentities() when actually displaying it on a page.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: htmlentities

    Hhaa.. finally I got it! whew! thanks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width