Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Prevent SQL Injection with PHP

Featured Replies

Just wrote this and thought I'd throw it up here, I know I'm not that active anymore but if I can help out in any way that's a plus :).

 

http://thisismedia.co.uk/ben/2011/03/preventing-sql-injection-in-ph/

Very nice topic, but technally a simple function as below would prevent injections quite well

 

function clean_data($data){

global $mysqli;

return mysqli_real_escape_string(trim(htmlentities($data,ENT_QUOTES,'UTF-8')));

}

 

then use that on any data going into mysql

 

also filtering things that require int only to int only

 

$id = $_GET['id'];
$id = preg_replace('#[^0-9]#i', '', $id);

Edited by webdesigner93

  • Author

Very nice topic, but technally a simple function as below would prevent injections quite well

 

function clean_data($data){

global $mysqli;

return mysqli_real_escape_string(trim(htmlentities($data,ENT_QUOTES,'UTF-8')));

}

 

then use that on any data going into mysql

 

also filtering things that require int only to int only

 

$id = $_GET['id'];
$id = preg_replace('#[^0-9]#i', '', $id);

 

I was going to write about casting to integers etc but I'm trying to keep it simple. Add it to the tutorial in the comments if you wish, might help someone using it :).

I was going to write about casting to integers etc but I'm trying to keep it simple. Add it to the tutorial in the comments if you wish, might help someone using it :).

I will deff do that :) u should write more blog post, i noticed u don't have many seems like a cool blog that i'd read if it had more post :p

  • Author

I will deff do that :) u should write more blog post, i noticed u don't have many seems like a cool blog that i'd read if it had more post :p

 

Well at work we've been asked to blog a few times a week so the company appears more social. Should be able to keep a regular blog and get paid for the pleasure :D.

Just wrote this and thought I'd throw it up here, I know I'm not that active anymore but if I can help out in any way that's a plus :).

 

http://thisismedia.co.uk/ben/2011/03/preventing-sql-injection-in-ph/

 

Well written article. However, just use PHP PDO. PDO binding does this for you and with pdo binding SQL injections are near on impossible.

Well written article. However, just use PHP PDO. PDO binding does this for you and with pdo binding SQL injections are near on impossible.

I think personally prepared statements are a lazy way of doing things, and can also get people out of the habit of filtering there data properly, not just for sql statements but for app security in general, plus mysql_real_escape_string does a good job of preventing most sql injects without the extra work of binding params ect..

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.