Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to prevent SQL injection in PHP?

user-image
Question added by Muhammad Usman Usman , Software Enginner , Five Rivers Technologies
Date Posted: 2013/07/10
Ahsan Aslam
by Ahsan Aslam , Software Engineer , FiverRivers Technologies (pvt) Ltd

use a php function name 'mysql_real_escape_string()' but this function will be deprecated in5.5.0 version of php.
but you can use MySQLi or PDO_MySQL for prevent the sql injection in php.

Mohammad Shalabi
by Mohammad Shalabi , Solution Architect, Android, and IOS Architect , ALM New Way

Use parameterized queries

Deleted user
by Deleted user

If you maintaining a server, I would suggest you can hardened your PHP by using suhosin extensions in the project.
By default, FreeBSD is using this technology in PHP.
You can find the information on www.hardened-php.net I hope this help you

There are basically two main methods to prevent sql injection attacks. 

 

Make a function like this

 

<?php

function BlockSQLInjection($str)

{

return str_replace(array(“‘”,”””,”‘”,'”‘),array(“‘”,”&quot;”‘”,”&quot;”,$str));

}

?>

str_replace() function will replace all characters in the string

Another method is to use prepared statement to execute sql queries.

You can also prevent php sql injection attacks by removing unused stored procedures.

Zeeshan Mohammad
by Zeeshan Mohammad , Software Engineer in Research , Center for Collective Intelligence - Massachusetts Institute of Technology

You can use prepared statements.
These are sql statements that are parsed by the database server separate from the parameters so if one sends in some injected parameter value, then its effect is handled during parsing.

islam khalil
by islam khalil , Technical Manager , iCloudit

MYSQLi : Use mysql_real_escape_string($unsafe_variable); PDO :Use prepared statements and parameterized queries

Wali Farooqui
by Wali Farooqui , Student , Indian Institute of Information Technology and Management, Gwalior

using PDO $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute(array(':name' => $name)); foreach ($stmt as $row) { // do something with $row }

More Questions Like This