tags:

views:

599

answers:

9

Hello everyone. Can you please guide me which of the below code will be more efficient and fast.

$temp = $_REQUEST['s'];

OR

if (isset($_GET['s'])) {
  $temp = $_GET['s'];
}
else {
  $temp = $_POST['s'];
}

Thanks

A: 

Use REQUEST. Nobody cares about the speed of such a simple operation, and it's much cleaner code.

Steven Schlansker
Good answer, with the caveat that in many situations a GET or a POST should be picked based on the situation instead of using either one.
ceejayoz
You're right that nobody cares, but in my opinion using `$_REQUEST` is the wrong conclusion. See my answer.
Franz
why ussing $_REQUEST is cleaner compared with $_GET or $_POST? $_REQUEST performs the same logic behind the scene and picking either GET or POST gives you more control.
Jay Zeng
The claim that _REQUEST is more hygienic needs elaboration.
fsb
A: 

I would use the second method as it is more explicit. Otherwise you don't know where the variables are coming from.

Why do you need to check both GET and POST anyway? Surely using one or the other only makes more sense.

Orange Box
I've seen this before, with `GET` being used for only one item (e.g. moving it) and `POST` for multiple of them (a form with checkboxes...).
Franz
+11  A: 

$_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE.

But it's only a default, which depends on variables_order ; and not sure you want to work with cookies.

If I had to choose, I would probably not use $_REQUEST, and I would choose $_GET or $_POST -- depending on what my application should do (i.e. one or the other, but not both) : generally speaking :

  • You should use $_GET when someone is requesting data from your application.
  • And you should use $_POST when someone is pushing (inserting or updating ; or deleting) data to your application.

Either way, there will not be much of a difference about performances : the difference will be negligible, compared to what the rest of your script will do.

Pascal MARTIN
Ideally, you should always be able to use $_REQUEST. But that of course is only a perfect world.
Chacha102
A perfect world where XSS is not possible...
Matt Ellen
$_REQUEST is supposedly (or at least used to be) more expensive than using $_POST and $_GET directly.
Darrell Brogdon
+1 for the concept of the performance difference being negligible and the maintenance perspective being more important: $_GET and $_POST convey meaning in a way that $_REQUEST cannot.
Jon Cram
A: 

Don't worry. But you should still use the second solution (plus an extra check for none of those variables existing), because there are security issues with $_REQUEST (since $_GET and $_POST aren't the only sources for that array).

There was a post about the problems with $_REQUEST yesterday, I believe. Let me go find it.

EDIT: Oh well, not directly a post, but here it is anyway: http://kuza55.blogspot.com/2006/03/request-variable-fixation.html

Franz
+2  A: 

I'd suggest using $_POST and $_GET explicitly.

Using $_REQUEST should be unecessary with proper site design anyway, and it comes with some downsides like leaving you open to easier CSRF/XSS attacks and other sillyness that comes from storing data in the URL.

The speed difference should be minimal either way.

Daniel Bruce
A: 

You are prematurely optimizing. Also, you should really put some thought into whether GET should be used for stuff you're POST-ing, for security reasons.

Alex Brasetvik
Please don't try to tell folk that there's anything more secure about POST than there is about GET.
fsb
I did not. Point was that their uses should be given some thought and not blatantly used interchangeably, because "just typing REQUEST is so much easier".
Alex Brasetvik
If what you mean is that kobra should check that data was sent using the expected method, then I agree. Either of his code examples makes such testing impossible.
fsb
A: 

I only ever use _GET or _POST. I prefer to have control.

What I don't like about either code fragment in the OP is that they discard the information on which HTTP method was used. And that information is important for input sanitization.

For example, if a script accepts data from a form that's going to be entered into the DB then the form had better use POST (use GET only for idempotent actions). But if the script receives the input data via the GET method then it should (normally) be rejected. For me, such a situation might warrant writing a security violation to the error log since it's a sign somebody is trying something on.

With either code fragment in the OP, this sanitization wouldn't be possible.

fsb
Actually, it's dead simple to write a small page that posts whatever you want to a page. So unless you rely on referrer headers being sent, post vars aren't any safer than get vars. I suppose the biggest advantage of an explicit `$_POST` is to prevent search engine crawlers from doing something like this: http://thedailywtf.com/Articles/WellIntentioned-Destruction.aspx
Duroth
I said nothing to the contrary.What I said was that if the HTML form uses POST and the script handling it receives the form's data via GET then the script would want to know about it and not toss that fact away, as both kobra's example do.(Btw: referrer isn't safe either.)
fsb
A: 
if (isset($_GET['s'])) {
  $temp = $_GET['s'];
}
else {
  $temp = $_POST['s'];
}

Use that because it is safer and it won't make noticeable speed difference

Nick Brooks
A: 

I would use $_POST, and $_GET because differently from $_REQUEST their content is not influenced by variables_order.
When to use $_POST and $_GET depends on what kind of operation is being executed. An operation that changes the data handled from the server should be done through a POST request, while the other operations should be done through a GET request. To make an example, an operation that deletes a user account should not be directly executed after the user click on a link, while viewing an image can be done through a link.

kiamlaluno