#sql injection #php #mysql #security

How to Sanitize Input in PHP

Table Of Content


  1. Benefits:
  2. Uses:
  3. Function:
  4. Function Call:

Although the development has taken a great deal of up-gradation in the last few years with DevOps, frameworks, automation etc. etc. etc. but still there are a number of people who still prefer the old way. Moreover, if you are starting new of just creating a college project or something like that it’s still cool to use the core PHP.

The inputs in Core PHP are venerable to what you call “SQL Injection”, to before putting them to use it’s a mandatory practice to sanitize them. Below is the function you can use for the same. It’s simple, straight forward and easy to use.

Benefits:

  • It reduces the threat of SQL injection by removing the SQL (if there is any)
  • It also removes the HTML tags from the data passed as some bad guys pass <scripts> too for some undesirable works.

Uses:

Using the function is simple all you have to do is make sure that it needs mysql_connection links to function. Please make sure you provide it. Remaining is nothing to worry about.

Function:

/**
 * protecting  the input form the SQL injection and script injection.
 * @param $data
 * @return mixed
 */
public function sanitizer ($data)
{
    if (!empty($data)) {
        foreach ($data as $key => $dataValue) {
            if (empty($dataValue)) {
                continue;
            }
            $dataValue = strip_tags($dataValue);
            $data[$key] = mysql_real_escape_string($dataValue, $this->link);
        }
    }

    return $data;
}

Function Call:

$object = new Class();
$sanitizedGetArray = $object->sanitizer($_GET);
$sanitizedPostArray = $object->sanitizer($_POST);

If you have some other things to add to this function feel free to comment, I am still learning too you know.

Most Viewed Posts


Must Read


January 28, 2022

#AppImage #linux

How to Add AppImage Application to Menu in Ubuntu (Linux)

AppImages are the most easiest to run compared.The challenging task is to make the AppImage accessible globally through he system like an installed application (in the menu)

Reading time: less than a minute
How to Add Laravel Pint to VS Code Formatter
June 24, 2022

#code-quality #vscode #laravel-pint #laravel

How to Add Laravel Pint to VS Code Formatter

I have been using the unsatisfactory formatters from a long time. All of them have one thing but lack other. Laravel Pint is one who has it all, combining it with vscode is like proving you laravel project a super power.

Reading time: 2 minutes
March 24, 2021

#express js #node #node js #sequelize #mysql

How to create model association in Sequelize - express JS / Node JS

In the very initial days of my work in Node JS if faced this issue. Though Sequelize provide a great ORM for MySQL but the association within the models is a bit tricky.

Reading time: 2 minutes
December 14, 2021

#database #javascript #sequelize #npx

Sequelize CLI Commands

list of Sequelize CLI commands which comes handy, while working on a project.

Reading time: 1 minute
How to create a progressive web app (PWA) in laravel 5.* and above
July 07, 2019

#functionality #pwa #javascript #laravel

How to create a progressive web app (PWA) in laravel 5.* and above

The progressive web app is the new trend as well as the need for the present time. I hope before getting in the “HOW” part you know what is exactly a PWA

Reading time: 4 minutes
March 24, 2021

#express js #node #node js #sequelize #mysql

How to create model association in Sequelize - express JS / Node JS

In the very initial days of my work in Node JS if faced this issue. Though Sequelize provide a great ORM for MySQL but the association within the models is a bit tricky.

Reading time: 2 minutes
April 22, 2019

#javascript #validation #security

Client-side validation before uploading an excel file

Validation is one of the major parts of web development. But sometimes it can be a ball buster especially when it comes to validating file uploads.

Reading time: less than a minute
September 14, 2018

#functionality #authentication #cakephp #php

Auto login in CakePHP 3.x

Although the authentication component of CakePHP 3 has made the life of a developer much easier but still there are some functionalities that are still needed to be done manually. One of them is login automatically if the users have checked for 'remember me'.

Reading time: 3 minutes
November 02, 2020

#blog #php #laravel

How to get started with laravel wink for creating a blog

There are a number of options available for purpose of blog creation but if you are a developer non of them are as satisfying as laravel wink.

Reading time: 1 minute
December 20, 2018

#functionality #log #cakephp #php

How to make log daily in CakePHP 3.x

As you may know according to the default configuration of CakePHP the debug and error log is generated in the same file. But this can be changed in a few simple steps

Reading time: less than a minute
mohammed-samgan-khan

Hi, I am Samgan, I know the adds are bit annoying but i hope you understand. Talking about me, I excel at solving complex problems involving logic and step by step breakdown of the problem. Besides, to develop complex algorithms, I specialize in problem framing, systems design, and product development strategy. Sometimes I also enjoy public speaking.