Solved

html special chars

Posted on 2013-12-03
11
276 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:burnedfaceless
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:burnedfaceless
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for answering my question
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now