fareforce Posted August 21, 2009 Share Posted August 21, 2009 I have never been good with looping, but I know there has to be a better way other than what I am doing. I have an html form that has a lot of fields, and is using the POST method, and sending it to my php file. In my php file I currently have $First_Name = $_POST[First_Name']; $Last_Name = $_POST[Last_Name']; $Address = $_POST['Address']; $City = $_POST['City']; $State = $_POST['State']; $Zip = $_POST['Zip']; $Phone = $_POST['Phone']; $Mobile = $_POST['Mobile']; $email = $_POST['email']; ....etc. and it works fine, but I am just trying to clean it up a little. What is a better way of doing this? Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/ Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 You could use extract() ie extract($_POST); echo $First_Name; Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903580 Share on other sites More sharing options...
fareforce Posted August 21, 2009 Author Share Posted August 21, 2009 You could use extract() ie extract($_POST); echo $First_Name; Does extract() automatically set all of the variables to the html form input id? (ie. $first_name, $last_name, etc? Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903588 Share on other sites More sharing options...
fareforce Posted August 21, 2009 Author Share Posted August 21, 2009 I checed it out, and it works great! Thank you much! I guess I was trying to do it the hard way! Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903593 Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 Well personally I do it the hard way.. and rarely use extract. I find i either use $_POST[First_Name'] or I filter it before setting it to a variable. Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903596 Share on other sites More sharing options...
thebadbad Posted August 21, 2009 Share Posted August 21, 2009 It's a bad idea to use extract() on user input like the $_POST array, at least when it's used without a safer flag than the default EXTR_OVERWRITE. E.g. if your script looks like this (worst case scenario): <?php $admin = false; extract($_POST); if ($admin) { //admin access } ?> A user could simply POST admin=something to the script and gain admin access. Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903603 Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2009 Share Posted August 21, 2009 Edit: Basically says the same as above ^^^ If you are going to use extract(), set the second parameter to EXTR_PREFIX_ALL and use a unique prefix (3rd parameter.) This will cut down on the number of hackers breaking into your script because extract() without doing that will allow a hacker to set any of your existing program variables to anything he wants. So if you have code like $admin = 1; (a common variable name used to indicate you are an administrator to your script) a hacker could just set that by providing a POST variable by that same name and value. Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903607 Share on other sites More sharing options...
thebadbad Posted August 21, 2009 Share Posted August 21, 2009 without doing that will allow a hacker to set any of your existing program variables to anything he wants. But only if your variables are set before running extract(), or if they aren't initiated before use (just to clarify). Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903620 Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 WOW i'm kinda shocked i forgot to add EXTR_SKIP Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903625 Share on other sites More sharing options...
newbtophp Posted August 21, 2009 Share Posted August 21, 2009 Hmm Im wanting to code something using a similar technique, do you mind taking a look? http://www.phpfreaks.com/forums/index.php/topic,266075.msg1254768.html Link to comment https://forums.phpfreaks.com/topic/171337-solved-_post-looping-help/#findComment-903628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.