html special chars

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?
burnedfacelessAsked:
Who is Participating?
 
Cornelia YoderConnect With a Mentor ArtistCommented:
It does change the variable, so that

<abc.def>

becomes

&#60abc&#46def&#62


http://www.ascii.cl/htmlcodes.htm
0
 
Ray PaseurCommented:
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
 
Cornelia YoderArtistCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
burnedfacelessAuthor Commented:
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
 
burnedfacelessAuthor Commented:
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
 
Cornelia YoderArtistCommented:
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
 
burnedfacelessAuthor Commented:
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
 
burnedfacelessAuthor Commented:
Thank you for answering my question
0
 
Ray PaseurCommented:
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
 
burnedfacelessAuthor Commented:
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
 
burnedfacelessAuthor Commented:
P.s. I have to work on the android tablets today. You really are a genius who saved me hours of work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.