Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

htmlspecialchars() tutotrial for beginners ~[Step by Step | Pictures]~

Here is my tutorial how to setup htmlspecialchars() and protect your site to xss vulnerability.
If you didn't know what XSS vulnerability is then i recommend you read this tutorial first, https://hackforums.net/showthread.php?tid=2910051

What you need, before you start read tutorial?
  • Website or other think to run php files (ex. XAMPP)

  • Notepad++ or other program to edit php files.



Steps,
  1. First make 2 php file, "index.php" and "without.php".
    Open "index.php" and build simple form page. Like this,
    Spoiler (Click to View)

  2. When "index.php" is ready, open "without.php" file and make it looks like this,
    Spoiler (Click to View)

  3. Now when you type something in "index.php" its send it to "without.php" page. So if i type in textbox script,
    Code
    <script>alert(1)</script>

    Then i press send..
    Spoiler (Click to View)


  4. Now we just need add htmlspecialchars() function in "without.php" file,
    Spoiler (Click to View)


  5. Lets try again, what happen if i try send same script,
    Spoiler (Click to View)


  6. Now htmlspecialchars() function change all <, > etc to html code and xss vulnerability is fixed.
    Congratulations! 🎉


Hope this tutorial help you understand how to use htmlspecialchars() function,
Good luck and have fun!

~ DogiD🤝
Very nice tutorial, well explained mate.
thank you for this! Well explained and confirmed what I knew :)

Using this for my site
goodbye
(Dec 24, 2012 05:40 AM)redpois0n Wrote:  thank you for this! Well explained and confirmed what I knew :)

Using this for my site

Good to hear ;)
Thank you,

~ DogiD🤝
Have learned a good thing. Thanks for the share.
You forgot to mention htmlspecialchars is only used while parsing the content.
Do not use htmlspecialchars then save a content to a db then parse it.
ScanYourFiles - Under development - will be out soon
woow very good and detailed tutorial , many have knowledge leak when it comes to these stuff ...
Nice tut....it will be better...if this was a video...
I use htmlentities() myself but great tut anyway.
(Dec 24, 2012 10:15 AM)what else? Wrote:  You forgot to mention htmlspecialchars is only used while parsing the content.
Do not use htmlspecialchars then save a content to a db then parse it.

Yes this exactly.

However I myself do use htmlspecialchars when saving to db for certain things, such as usernames for my website. This is fine. Otherwise, parse on output is the recommended practice.

(Dec 24, 2012 08:23 PM)Volim te Wrote:  I use htmlentities() myself but great tut anyway.

If you allow certain tags with htmlentities(), eg <b>, ensure that you are aware of the fact that people can still execute scripts.

Eg:
<b onload='alert(1)'></b>

To the OP:
Well done and thanks for the tutorial :) This should help a lot of people secure their websites.

[Image: IRRJ3bf.png]

You are a 0, I am the 1.
Why make this thread? You can just look at php's site. http://php.net/manual/en/function.htmlspecialchars.php
[Image: eh4TL.gif]
(Dec 24, 2012 08:27 PM)VoidEx. Wrote:  Yes this exactly.

However I myself do use htmlspecialchars when saving to db for certain things, such as usernames for my website. This is fine. Otherwise, parse on output is the recommended practice.

If you allow certain tags with htmlentities(), eg <b>, ensure that you are aware of the fact that people can still execute scripts.

Eg:
<b onload='alert(1)'></b>

To the OP:
Well done and thanks for the tutorial :) This should help a lot of people secure their websites.
I mostly add a number is `disallowed` words like javascript, onload, onerror ,document.cookie etc.
(Dec 24, 2012 08:27 PM)VoidEx. Wrote:  However I myself do use htmlspecialchars when saving to db for certain things, such as usernames for my website. This is fine. Otherwise, parse on output is the recommended practice.

You know why its the recommended practice?
Because it makes sense.
You should never store escaped data, I mean, what if you want to export it to a CSV file, you'll have HTML escapes all over.
On the 5th step the image should say "With htmlspecialchars()" ithink.

Nice tutorial though :)
[Image: H4kDu.png]
.. You made a tutorial for a DOCUMENTED function?
.. Why?

http://php.net/htmlspecialchars


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Node.JS] by VecturaAug 5, 2025 06:29 PM 3 125 Aug 5, 2025 06:29 PM
Last Post: HerrWinston
[PHP] by FiendJan 23, 2018 12:06 PM 3 126 Jan 23, 2018 12:06 PM
Last Post: Fiend
[PHP] by TRH4CKFeb 23, 2014 07:00 AM 4 192 Feb 23, 2014 07:00 AM
Last Post: MicroCoil