Web Design Forum: Prevent SQL Injection with PHP - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Prevent SQL Injection with PHP Rate Topic: -----

#1 User is offline   Cabbage 

  • That's what she said
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,333
  • Joined: 04-February 08
  • Reputation: 7
  • Gender:Male
  • Location:Derby
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 28 March 2011 - 12:59 PM

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.c...njection-in-ph/
0

#2 User is online   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 28 March 2011 - 01:26 PM

View PostCabbage, on 28 March 2011 - 12:59 PM, said:

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.c...njection-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);


This post has been edited by webdesigner93: 28 March 2011 - 01:27 PM

0

#3 User is offline   Cabbage 

  • That's what she said
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,333
  • Joined: 04-February 08
  • Reputation: 7
  • Gender:Male
  • Location:Derby
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 28 March 2011 - 01:48 PM

View Postwebdesigner93, on 28 March 2011 - 01:26 PM, said:

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 :).
0

#4 User is online   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 28 March 2011 - 02:14 PM

View PostCabbage, on 28 March 2011 - 01:48 PM, said:

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
0

#5 User is offline   Cabbage 

  • That's what she said
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,333
  • Joined: 04-February 08
  • Reputation: 7
  • Gender:Male
  • Location:Derby
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 29 March 2011 - 11:36 AM

View Postwebdesigner93, on 28 March 2011 - 02:14 PM, said:

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.
0

#6 User is offline   Avera 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 432
  • Joined: 16-November 08
  • Reputation: 1
  • Location:Manchester
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 29 March 2011 - 08:54 PM

View PostCabbage, on 28 March 2011 - 12:59 PM, said:

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.c...njection-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.
0

#7 User is online   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 30 March 2011 - 04:13 AM

View PostAvera, on 29 March 2011 - 08:54 PM, said:

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..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users