supertrucker Posted April 10, 2008 Share Posted April 10, 2008 Have i started a bad habit by using $_REQUEST for all my post and get forms? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 10, 2008 Share Posted April 10, 2008 Totally. All the cool kids use $_GET and $_POST. /*get a variable from post or get*/function form_var($str){ $retval = ''; if (isset($_POST[$str])) $retval = $_POST[$str]; elseif (isset($_GET[$str])) $retval = $_GET[$str]; if (is_array($retval)) { foreach ($retval as $k => $v) { $retval[$k] = trim($v); if (get_magic_quotes_gpc()) $retval[$k] = stripslashes($v); } } else { $retval = trim($retval); if (get_magic_quotes_gpc()) $retval = stripslashes($retval); } return $retval;}$val = form_var('name'); Link to comment Share on other sites More sharing options...
supertrucker Posted April 10, 2008 Author Share Posted April 10, 2008 Lovely, thanks for the tip!St wanders off to rewrite his scripts... Link to comment Share on other sites More sharing options...
hgmme@wa Posted April 10, 2008 Share Posted April 10, 2008 Totally. All the cool kids use $_GET and $_POST.So if it's "cooler" to user $_GET and $_POST what then is the purpose of $_REQUEST and when should it be used? Link to comment Share on other sites More sharing options...
boen_robot Posted April 10, 2008 Share Posted April 10, 2008 So if it's "cooler" to user $_GET and $_POST what then is the purpose of $_REQUEST and when should it be used?What is the purpose of evil? To show what means not to be good. In the same fashion, the purpose of $_REQUEST is to show what is not cool . Link to comment Share on other sites More sharing options...
justsomeguy Posted April 10, 2008 Share Posted April 10, 2008 The purpose of $_REQUEST is to separate the l33t from the n00bs. Just think about what it's doing though. It combines $_GET, $_POST, and $_COOKIE. If you are looking for a variable submitted from a form, why would you check the cookies? It doesn't make sense to do that. It would mean that the value in the cookie might overwrite a value from the form. So if you gave someone a cookie and it has a variable in it called "username", and they are trying to log in (or add a new user) and the form has a variable called "username", if you check $_REQUEST['username'] then you might be getting the value from the cookie when you're trying to get the value from the form (whether or not you actually do is server-dependent). If you're trying to get a value from a form, then use something that only checks get and post, like the function above. The function above gives preference to post, so if a value is in both get and post it will use the one from post. It doesn't even look at cookies. The function above also trims the value and strips slashes if they were added, and it works with arrays also. Link to comment Share on other sites More sharing options...
hgmme@wa Posted April 10, 2008 Share Posted April 10, 2008 Ok that makes sense. Thanks for the clarification. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.