Jump to content

Why use $_GET/$_POST?


supertrucker

Recommended Posts

Have i started a bad habit by using $_REQUEST for all my post and get forms?

Link to comment
Share on other sites

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

Lovely, thanks for the tip!St wanders off to rewrite his scripts...

Link to comment
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...