TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-29-2007, 11:42 AM   #1 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default Secure user submitted queries?

Hello,

I'd like to know what needs to be done to secure user submitted queries.
Is using mysql_real_escape_string for each $_POST and $_GET var enough?
Or should I do more?

Thanks!
Sled is offline  
Reply With Quote
Old 10-29-2007, 07:21 PM   #2 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

PHP Code:
function clean($str) {
return 
htmlspecialchars(mysql_real_escape_string(stripslashes($str)));

Execute:
PHP Code:
clean($_POST['var']); 
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 10-29-2007, 11:58 PM   #3 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default

Ok, thanks, and once I want to get it from the database, any functions I need to use so it shows up properly?

Thanks!
Sled is offline  
Reply With Quote
Old 10-30-2007, 12:16 AM   #4 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Wales - UK
Posts: 8
Thanks: 0
Dorza is on a distinguished road
Default

This is what I use my self...

PHP Code:

//I send pretty much everything through the following when entering data into the database:
function safeAll($string
{
  
$string trim($string);
  
$string mysql_real_escape_string($string);
  
$string htmlentities($stringENT_QUOTES);
  return 
$string;
}

//Coming out of the database I run appropriate data through this:

function convertHtml($string
{
   return 
html_entity_decode($stringENT_QUOTES);
}


//Clean input:
safeAll($_POST/GET['text_to_db']);

//Convert output:
convertHtml($output['output_txt_from_db_query']); 
If there is anything wrong with this or if there can be any additions to the functions then please let me/others know about it.:)

Also if your expecting a number to be submitted either via POST or GET then I personally do the following:

PHP Code:
if(!ctype_digit($_GET[id]))  
{
  
//Above: if you did:  if(!(int)$_GET[id]) then something such as 5k55  or 5.5 would get through
  //ctype_digit will ONLY accept whole numbers.
  
echo "A nice error message";
}
else
{
   
carry on doing what it is you want to do.

For me securing and validating user input is what takes most of the time when scripting with PHP. It can be quite involved, but if done correctly (hope mine are ok) you should have pretty secure scripts/systems.

Last edited by Dorza : 10-30-2007 at 12:48 AM.
Dorza is offline  
Reply With Quote
Old 10-30-2007, 01:02 AM   #5 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default

I've been using is_numeric for the second job.
Both functions suggested look simular, I guess they'll be secure :)
Sled is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:13 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design