NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP: Best way to sanitize inputs

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-25-2008, 12:27 PM THREAD STARTER               #1 (permalink)
AzN
is on hiatus
Join Date: May 2006
Posts: 2,453
AzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond repute
 


Find Marrow Donors! Ethan Allen Fund Ethan Allen Fund Ethan Allen Fund Save a Life Save a Life Save a Life Save a Life Save a Life Save a Life VA Tech Memorial VA Tech Memorial VA Tech Memorial VA Tech Memorial Save a Life Save a Life Save a Life

PHP: Best way to sanitize inputs


Been reading about mysql injections, so I'm wondering what's the best way to sanitize inputs to protect against injection in PHP?

I've read at a place converting strings into hex or base64 encoded strings can be a good alternative.

Thanks.
__________________
Currently on hiatus. Back whenever.
AzN is offline  
Old 07-25-2008, 01:14 PM   #2 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




This is the best way:
PHP Code:
$safe mysql_real_escape_string('blah'); 
????: NamePros.com http://www.namepros.com/programming/496674-php-best-way-to-sanitize-inputs.html
You need to be connected to a MySQL server, but it works great.
Barrucadu is offline  
Old 07-25-2008, 01:24 PM THREAD STARTER               #3 (permalink)
AzN
is on hiatus
Join Date: May 2006
Posts: 2,453
AzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond reputeAzN has a reputation beyond repute
 


Find Marrow Donors! Ethan Allen Fund Ethan Allen Fund Ethan Allen Fund Save a Life Save a Life Save a Life Save a Life Save a Life Save a Life VA Tech Memorial VA Tech Memorial VA Tech Memorial VA Tech Memorial Save a Life Save a Life Save a Life
Originally Posted by Barrucadu
This is the best way:
????: NamePros.com http://www.namepros.com/showthread.php?t=496674
PHP Code:
$safe mysql_real_escape_string('blah'); 
You need to be connected to a MySQL server, but it works great.
Barrucadu, I've read that mysql_real_escape_string() only adds a backslash and does not protect against everytype of sql injection, is this correct?

Just wondering what are the various options,

Thanks,
AzN
__________________
Currently on hiatus. Back whenever.
AzN is offline  
Old 07-25-2008, 01:38 PM   #4 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



I use a variety of ones, such as :

$info = trim($info);
$info = strip_tags($info);
$info = stripslashes($info);
$info = mysql_real_escape_string($info);
Albino is offline  
Old 07-25-2008, 04:12 PM   #5 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



this is what i use:

PHP Code:
function sql_quote($value) {
    
$value strip_tags($value);
    if(
get_magic_quotes_gpc()) {
          
$value stripslashes$value );
????: NamePros.com http://www.namepros.com/showthread.php?t=496674
    }
    
//check if this function exists
    
if(function_exists("mysql_real_escape_string")) {
          
$value mysql_real_escape_string$value );
    }
    
//for PHP version < 4.3.0 use addslashes
    
else {
          
$value addslashes$value );
    }
    return 
$value;

nasaboy007 is offline  
Old 07-25-2008, 04:50 PM   #6 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
http://www.php.net/manual/en/securit...-injection.php

www.php.net/manual/en/intro.filter.php
Eric is offline  
Old 07-25-2008, 10:30 PM   #7 (permalink)
NamePros Member
Join Date: Sep 2006
Posts: 99
Bruce_KD will become famous soon enoughBruce_KD will become famous soon enough
 



The best way to prevent injection is to hack yourself (or at least think about it).
If you're expecting numerical input, don't could on a user to make it numerical.

Take a look at the following snippet:
PHP Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=496674
include 'mysql_connect.php';
if (
$_GET['start'] && $_GET['end']) {
  
$start mysql_real_escape_string($_GET['start']);
  
$end mysql_real_escape_string($_GET['end']);
  
$query mysql_query("Select name, rank from players where rank between $start and $end");
  while (
$row mysql_fetch_array($queryMYSQL_NUM)) 
????: NamePros.com http://www.namepros.com/showthread.php?t=496674
    echo 
$row[0] . ": #".$row[1];
}
else 
  echo 
"Enter the Starting and ending ranks";
?>
This code is great right? I used mysql_real_escape_string and everything!
What happens if I enter '1' for start and '2 union Select name, password from players' for end?
I get the username and password for every entry in the table!

What did we do wrong here?

1. Obviously, we didn't use single quotes inside the mysql query. If you use single quotes surrounding every variable AND use mysql_real_escape_string, you're much safer.

2.
We trusted user input. We didn't check if both $start and $end were numerical. We could have even been lazy and done $start = floor($start).
You'd be better off using is_int($start) at least is_numeric($start)

3. The code is lazy. It doesn't check if $Query actually worked, which can throw a mysql_fetch_array warning if there are no results. But this is because I was lazy writing my example, not trying to make a point.


So the whole point of this was to show you there isn't one fool-proof way to stop mysql injection. mysql_real_escape_string does not work every time.
You need to use your brain and understand how injection really works before you can defend against it.


Bruce
Bruce_KD is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 04:02 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger