Welcome friends in both of our previous tutorials we have learned how we can perform SQL injection and login into system where we don’t know the password actually in this tutorial I’ll teach you how to prevent from such SQL injections for that let’s take a look at our login script as I said before to understand this tutorial completely you.

Should have the knowledge of PHP if you don’t know PHP you can check our tutorial on PHP and so you will have the complete knowledge about what is going on here so let me tell.

You this statement connects to our database this statement selects the database then this part is responsible for checking whether the user name and password entered are.

Correct or incorrect this is our input of username and this is our input of password we are assigning this value to you name variable and we are assigning the password value to pass variable.

And this is our SQL query it is select count you name from users which is our table name where you name is equal to this variable you.

Name and password is equal to this variable password that means it checks whether of a given user name and password exists in database and if it exists this query.

Occurrence of our given criteria now they’re firing this query and we are fetching this result now let’s come here if.

The result is 0 that means there is 0 rows that contains the same username and password it says invalid login and if there are more than 0 rows that mean one or more rows that contains the same username and password then it will return correct login and as I said this query.

Debug is for the debug purpose of our SQL statement so this.

Is the internal mechanism of our login script so what is exactly happening when someone tries to inject SQL it is taking this whole part as comment and SQL is not processing this part that means if you have the correct username you can login so how we can prevent from this is we can restrict user from entering.

Such control characters and doing this is very easy we can restrict users for entering such control characters by a.

Very useful command of our MySQL library in PHP which is MySQL escape string and what it does.

Is it actually escape all the control characters from the string so it prevent our SQL query to be injected by someone else save this and let’s reload this it asked us to repeat that action and click on resend and as you can see.

It says invalid login to debug our SQL statement let’s have a look query till here seems to be ok but this backward slash is being entered automatically by.

Our command which was MySQL escape string it is actually a method which escapes the strings from our MySQL query so it checks character sets of our.

Database and it removes all the control characters from the query so our given input is this because it neglect this quote by this backward slash and hence we are saved so remember to prevent from SQL injection we can simply use a very useful method which is MySQL escape string if you are working with any other language than PHP you can also use same kind of logic to save your script thank you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here