Solved

html special chars

Posted on 2013-12-03
11
281 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 109

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:yodercm
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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:yodercm
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:
yodercm earned 500 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 109

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

777 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