?
Solved

html special chars

Posted on 2013-12-03
11
Medium Priority
?
305 Views
Last Modified: 2013-12-17
Does it alter the variable

i.e.

$variable = ($_POST['vairable']);

then

      htmlspecialchars($variable, ENT_QUOTES, 'UTF-8'). '!';

Does that protect against html.

So any further use of that variable would be protected right?

Or does it need to be used for output?
0
Comment
Question by:burnedfaceless
  • 6
  • 3
  • 2
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39694027
Please get yourself an introductory book on the basics of PHP.  This is a good one.  You need to give yourself time to get a foundation in how the language works.
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899

HTMLSpecialChars() is used on the output side of the processing before you echo any data to the browser.  In a well-designed application it would never be used on any data that is stored, just on the output.
http://php.net/manual/en/function.htmlspecialchars.php

You do not "protect against HTML" -- the point of the function is to prevent external data from injecting malicious javascript or similar into your web site in a way that would cause the web site to send the malicious script to a client machine.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 39694362
While what RayPaseur said is the original intention of htmlspecialchars(), it is extremely useful for protecting your database from malicious data.

I simply run every input through it to change all the special characters to their html code equivalents BEFORE I put the inputs into a database.  This is a very easy way to protect against malicious html.

The only downside is that each special character takes up to 5 bytes to store in a database field, but the upside is that NO malicious data ever gets into your database, so you don't have to worry about using it in any fashion as output.

By using htmlspecialchars once on input, you don't have to use it many other times on output, and you can't forget to use it on an output and thereby expose your data to misuse.
0
 

Author Comment

by:burnedfaceless
ID: 39694380
Ray I purchased the book you recommended two years ago. That is the basis of this question, it [htmlspecialchars] has been used only in writing (echo).

It's effect seems less visible on the post method or I am doing something wrong.

Please answer the question. That is what this website is for.

I actually bought both the books you recommended. I'm hitting them hard.

Ed. Note: I did answer the question, right at the top, but you're not far enough along in your understanding of PHP to recognize the answer!  And that's why I recommend that you get some knowledge foundation in PHP.  It's one thing to buy a book; it's very much another to actually read it and work through the code examples.  Two months of deliberate and structured study will save you from two years of trial-and-error, missed deadlines, failed applications and general frustration!  And at the end of the two months you'll be more than two years ahead.  I teach PHP for a living and I see it all the time in the contrast between my students and the novice community in the many PHP forums on the WWW.  The ones who concentrate their learning leap ahead, while the ones who just copy code samples without any deeper understanding of the design and thought processes get left behind.  So don't get left behind -- try to understand, not just copy.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:burnedfaceless
ID: 39694383
By using htmlspecialchars once on input, you don't have to use it many other times on output, and you can't forget to use it on an output and thereby expose your data to misuse.

Is this what the above code does?
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 39694431
Yes, it is exactly what it does.  It changes the input special characters to their html code equivalents, so they can never be used for anything but safe output.
0
 

Author Comment

by:burnedfaceless
ID: 39694432
I am going to start working on AJAX when the book comes in. It does what I need.

They're all really the same. Even the syntax thought process is exactly the same.

Interactions are the hard part but...focus on that...
0
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 2000 total points
ID: 39694436
It does change the variable, so that

<abc.def>

becomes

&#60abc&#46def&#62


http://www.ascii.cl/htmlcodes.htm
0
 

Author Closing Comment

by:burnedfaceless
ID: 39694447
Thank you for answering my question
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39723823
To anyone reading this question in the future, I recommend against using the htmlspecialchars() function in the manner described here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28309425.html#a39694362
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28309425.html#a39694436

If you're contemplating doing something like that, contact your lead programmer or the other programmers on your team to be sure you're all on the same page about the standards.  This function mungs the data.  If you do that before you write it into the data base, and the other programmers naturally assume that they need to use htmlspecialchars() on the output side, which is the way it was intended, then your data is going to be passed through htmlspecialchars() twice.  You probably do not want to do that.

You probably also want to read the user-contributed notes here, before you assume that this function will be compatible with the character set encoding of your data base.  PHP has changed how this function works.
http://www.php.net/manual/en/function.htmlspecialchars.php
0
 

Author Comment

by:burnedfaceless
ID: 39724191
Ray thanks for your input.

You obviously know way more about this than me, and most people on this website. So is my use acceptable? If you're trying to teach me something I'm missing it.

I'm really looking forward to the computer science classes.

I've noticed that song meanings emails me replies to my comments that have been run through htmlspecialchars. Of course they're not malicious but you can notice small alterations.
0
 

Author Comment

by:burnedfaceless
ID: 39724198
P.s. I have to work on the android tablets today. You really are a genius who saved me hours of work.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question