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-28-2009, 01:23 AM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default $_POST: Are both approaches equally good?

Take a look at the code below regarding POST. Which is better?

PHP Code:
$sql "SELECT   teachername
               , statecourse
               , semester
               , meetingcode
               , last_name
               , first_name
          FROM 
$c_tblname
         WHERE teachername = \"
{$_POST['teachername']}\" and
               semester    = \"
{$_POST['semester']}\"    and
               meetingcode = \"
{$_POST['meetingcode']}\" ";

//------------------------------------------

extract ($_POST) ;
$sql "SELECT   teachername
               , statecourse
               , semester
               , meetingcode
               , last_name
               , first_name
          FROM 
$c_tblname
         WHERE teachername = '
$teachername' and
               semester    = '
$semester'    and
               meetingcode = '
$meetingcode' "
Thanks!
Dave is offline  
Reply With Quote
Old 10-28-2009, 02:25 AM   #2 (permalink)
The Wanderer
Newcomer 
 
etoolbox's Avatar
 
Join Date: Dec 2008
Location: Auckland, NZ
Posts: 24
Thanks: 0
etoolbox is on a distinguished road
Default

They're both equally bad from the perspective of SQL injection: http://en.wikipedia.org/wiki/SQL_injection

Even if there's no intentional SQL injection your query will error out if any of the submitted data contains a " in the first example or a ' in the second.

Once you've learned about escaping user submitted data, I would recommend *not* using extract. The PHP manual page itself ( http://www.php.net/extract ) recommends "Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.)"

There's no sense using extract() anyway, and it may overwrite variables you have already set up, further opening your code up to abuse.
__________________
Chris Hope's LAMP Blog: http://www.electrictoolbox.com/
etoolbox is offline  
Reply With Quote
The Following User Says Thank You to etoolbox For This Useful Post:
Dave (10-28-2009)
Old 10-28-2009, 02:49 AM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks. I had read the PHP manual advisory previously, but since then I have also read a couple of blogs about how great extract() was. But I hadn't thought about it overwriting existing variables, which is not a good thing.

Dave
Dave is offline  
Reply With Quote
Old 10-28-2009, 09:05 AM   #4 (permalink)
The Wanderer
Newcomer 
 
etoolbox's Avatar
 
Join Date: Dec 2008
Location: Auckland, NZ
Posts: 24
Thanks: 0
etoolbox is on a distinguished road
Default

Funnily enough I've just read a post which talks about SQL injection along with some examples: http://blog.tuvinh.com/top-7-php-security-blunders/

Some other useful stuff in there too.
__________________
Chris Hope's LAMP Blog: http://www.electrictoolbox.com/
etoolbox is offline  
Reply With Quote
The Following User Says Thank You to etoolbox For This Useful Post:
Dave (10-30-2009)
Old 10-28-2009, 09:43 AM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Originally Posted by etoolbox View Post
There's no sense using extract() anyway, and it may overwrite variables you have already set up, further opening your code up to abuse.
That problem is solved by using the EXTR_PREFIX_SAME flag.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (10-30-2009)
Old 10-28-2009, 10:02 PM   #6 (permalink)
The Wanderer
Newcomer 
 
etoolbox's Avatar
 
Join Date: Dec 2008
Location: Auckland, NZ
Posts: 24
Thanks: 0
etoolbox is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
That problem is solved by using the EXTR_PREFIX_SAME flag.
Good point. I've also had another look at the docs and you can also specify what to do with collisions with the extract_type parameter. I still wouldn't use this function myself though.
__________________
Chris Hope's LAMP Blog: http://www.electrictoolbox.com/
etoolbox is offline  
Reply With Quote
The Following User Says Thank You to etoolbox For This Useful Post:
Dave (10-30-2009)
Old 10-29-2009, 02:19 AM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Extract is a handy little fella in some situations, no sense in ruling it out; I've written numerous functions in the past where I wanted/needed to return multiple arrays of data. Nest the arrays, return it, and extract it on the other side into it's components. No fuss.
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (10-30-2009)
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML only good for RSS? Orc XML, XSLT, XPath, XQuery 12 10-19-2012 10:03 AM
i need some good ideas about my website webtuto General 4 03-14-2008 12:33 PM
Good business communication. Village Idiot The Lounge 10 01-11-2008 08:43 PM
Good 'n Fast thumbnail script?! marxx General 6 10-31-2007 11:22 AM
Good Evening obolus Member Introductions 4 10-07-2007 12:30 AM


All times are GMT. The time now is 02:29 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