• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Sanitizing input with PHP and JavaScript
Home
JavaScript

Sanitizing input with PHP and JavaScript

June 15th, 2025 Ivan Dimov JavaScript, PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

PHP

In PHP, there are a variety of ways in which you can sanitize the user input, depending on your needs. The htmlspecialchars function allows you to transform every special character in HTML into text so that the user cannot enter code in his input. It is good to use the function when you want to show a user input, stored or coming from GET parameters, on a page as it can be the only barrier between you and CSRF attacks. You can use htmlentities to convert all characters that have an equivalent as a HTML entity into the appropriate entity, not only the special characters used in code. For example:echo htmlentities(“©”); would output ©.

You can use the strip_tags function to remove all HTML tags from a string such as one coming from user input. This would essentially render it invulnerable to CSRF or embedding JavaScript or PHP logic in your application. However, as a second argument you may specify tags that are allowed. Be careful with allowing tags as users can add arbitrary attributes to those tags which can be malicious and harmful for your application.

If you wish to do the opposite, convert special HTML characters that are converted to entities back into normal HTML characters, you can use the html_entity_decode function.

With PHP, you would most likely be doing interactions with a database and have to sanitize any inputs passed to the interaction with the database; especially if it is a database using the SQL. For that purpose, you can use PDO and prepared statements. Prepared statements allow  you to add the inputs separately from the query itself, as parameters which would be utterly escaped in case the user wants to do dark things. The links above allow you to see how to start using PDO and prepared statements in your code today.

JavaScript

In JavaScript, you can perform simple sanitation of inputs by globally replacing the important HTML characters such as the less-than sign (<) and the greater-than sign (>) with their HTML entity equivalent manually. Here is a useful function that will get the job done:

function escapeHTML (unsafe_str) {

    return unsafe_str

      .replace(/&/g, '&amp;')

      .replace(/</g, '&lt;')

      .replace(/>/g, '&gt;')

      .replace(/\"/g, '&quot;')

      .replace(/\'/g, '&#39;'); // '&apos;' is not valid HTML 4

}

You can just display the user’s input on the page in a manner resembling the following:

$(“#msg”).html(escapeHTML($(“input”).val()));



// assuming we want to display the sanitized input in a container identified as msg.

References:

http://shebang.brandonmintern.com/foolproof-html-escaping-in-javascript/

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

Facebook Twitter Google+ LinkedIn Pinterest
Next article Create HTML5 Fullscreen Static Video Background Using CSS
Previous article How to Integrate PayPal Payment System in PHP & MySQL

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

5 Mistakes that make you look like a noob in PHP PHP
June 15th, 2025

5 Mistakes that make you look like a noob in PHP

12 Reasons to Choose PHP for Developing Website Guide
June 15th, 2025

12 Reasons to Choose PHP for Developing Website

How to Integrate PayPal Payment Gateway in Laravel 10: A Complete Guide Laravel
June 15th, 2025

How to Integrate PayPal Payment Gateway in Laravel 10: A Complete Guide

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • 5 Mistakes that make you look like a noob in PHP
  • 12 Reasons to Choose PHP for Developing Website
  • 3 reasons why you SHOULD write tests
  • How to Integrate PayPal Payment Gateway in Laravel 10: A Complete Guide
  • Javascript Image Compress using HTML5 Canvas & File API before Upload
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.