Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
FireATST
RavenNuke(tm) Development Team



Joined: Jun 12, 2004
Posts: 654
Location: Ohio

PostPosted: Wed Jun 02, 2010 7:48 pm Reply with quote

I need to update the following code and would like the pro's opinions on the best way....Smile

Code:
if($HTTP_POST_VARS) {

   foreach($HTTP_POST_VARS as $Key=>$Value) {
      $$Key = $Value;
   }
}

if($HTTP_COOKIE_VARS) {
   foreach($HTTP_COOKIE_VARS as $Key=>$Value) {
      $$Key = $Value;
   }
}

if($HTTP_GET_VARS) {
   foreach($HTTP_GET_VARS as $Key=>$Value) {
      $$Key = $Value;
   }
}

if($HTTP_SERVER_VARS) {
   foreach($HTTP_SERVER_VARS as $Key=>$Value) {
      $$Key = $Value;
   }
}

if($HTTP_ENV_VARS) {
   foreach($HTTP_ENV_VARS as $Key=>$Value) {
      $$Key = $Value;
   }
}


Should it be the following:

Code:
if(isset($_POST))   foreach($_POST   as $Key=>$Value) $$Key = $Value;


or should it be:

Code:
if($_POST)   foreach($_POST   as $Key=>$value) $$Key = $Value;


I only picked one out of the list and will change all to the correct style. Thank you.....Smile
 
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Wed Jun 02, 2010 8:17 pm Reply with quote

I think $_POST is inherently set so you don't have to do an if statement at all. Just do the foreach loop.

_________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. 
View user's profile Send private message
FireATST







PostPosted: Thu Jun 03, 2010 4:00 am Reply with quote

humm, will give that a go.....thanks Palbin....Smile
 
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Thu Jun 03, 2010 1:57 pm Reply with quote

If a form field is a checkbox and it is not filled out, no POST data is available for it to the receiving program. I don't know if this means that $_POST will not be set if the only field on the form was a checkbox. It would be worth verifying.

More to the point however, while the foreach loop can be very useful as diagnostic tool in developing a program to validate form input because you can see exactly what form fields are getting set and what they are set with, by the time you are ready to move into "production" you should be explicitly testing each form field and validating it for containing only the data it should contain. I'd say something like:
Code:


[if (isset($_POST['field1'])) {
  validate it
}


for each field. The validate it part should be the narrowest possible definition of the field. So, for instance, if it's an integer field you should validate for that. If it's supposed to be an integer between 1 and 100 validate for that. If it's text but html is not allowed validate that.

If there is anything showing up in your $_POST data that isn't from a legitimate field on your form you can be pretty sure someone has hacked your form (though our CSRF does a good job of preventing that). Likewise, if you have a text field on your form and it is not coming over as $_POST data you know that something has gone amiss with the form.


Last edited by fkelly on Mon Jun 07, 2010 5:59 pm; edited 1 time in total 
View user's profile Send private message Visit poster's website
gregexp
The Mouse Is Extension Of Arm



Joined: Feb 21, 2006
Posts: 1497
Location: In front of a screen....HELP! lol

PostPosted: Mon Jun 07, 2010 4:19 pm Reply with quote

I personally would use the following:

Code:


if(isset($_POST) AND (count($_POST) > 0)){
  extract($_POST, EXTR_PREFIX_ALL, "post");
  unset($_POST);
}



if you're wondering why I put the option to prefix everything, it's simple, stop it from overwriting existing variables.

_________________
For those who stand shall NEVER fall and those who fall shall RISE once more!! 
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©