192

Which of these code will be faster?

$temp = $_REQUEST['s'];

or

if (isset($_GET['s'])) {
  $temp = $_GET['s'];
}
else {
  $temp = $_POST['s'];
}
kobra
  • 4,853
  • 10
  • 32
  • 33
  • 6
    There is a third case, y'know. `!isset($_REQUEST['s'])`. – Franz Dec 17 '09 at 22:10
  • 5
    How important is it that other people understand your code clearly? POST and GET are explicit, whereas REQUEST could come from various sources. I think efficiency is negligible since REQUEST, POST, and GET superglobals are always loaded for each request. – Kevin Dec 17 '09 at 22:31

15 Answers15

296

$_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
  • 395,085
  • 80
  • 655
  • 663
  • 1
    Ideally, you should always be able to use $_REQUEST. But that of course is only a perfect world. – Tyler Carter Dec 17 '09 at 22:16
  • 2
    $_REQUEST is supposedly (or at least used to be) more expensive than using $_POST and $_GET directly. – Darrell Brogdon Dec 17 '09 at 22:47
  • 3
    +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 Dec 17 '09 at 22:48
  • 11
    Using $_REQUEST doesn't cause XSS/XSRF. Not understanding the nuances of XSS/XSRF causes XSS/XSRF. As long as you mitigate with tokens, there's no problem AND you get the benefits of using $_REQUEST (all your variables are in one superglobal). I actually rebuild $_REQUEST before using it based on the other superglobals because of 'variables_order'. I process $_COOKIE, then $_GET, then $_POST. That way POST vars have the highest priority and cookie vars get the lowest, which allows me to implicitly fix a number of bugs (e.g. Adobe Flash and magic quotes). – CubicleSoft Oct 02 '13 at 14:03
  • its in the name, Get = get from , Post = post to – Grumpy Jun 12 '15 at 13:32
  • using a rest style, get would only contain the data to specify a resource (an auth token or a context token the exceptions), and post would be everything else (the contents of the resource for creation, hidden variables, etc. – Gerard ONeill Sep 10 '15 at 19:50
  • @MattEllen and upvoters - how can $_REQUEST be exploited by XSS/XSRF-attacks? – Toastgeraet Sep 09 '16 at 10:09
  • Probably something along the lines of: if someone sniffs a csrf token, then can inject it in the GET params if you're using $_REQUEST, or something. 7 years is a long time. PHP has probably changed a bit since then. – Matt Ellen Sep 09 '16 at 16:08
33

GET vs. POST

1) Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

2) Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

3) $_GET is an array of variables passed to the current script via the URL parameters.

4) $_POST is an array of variables passed to the current script via the HTTP POST method.

When to use GET?

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

GET may be used for sending non-sensitive data.

Note: GET should NEVER be used for sending passwords or other sensitive information!

When to use POST?

Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

Zee Ken
  • 349
  • 3
  • 6
27

$_GET retrieves variables from the querystring, or your URL.>

$_POST retrieves variables from a POST method, such as (generally) forms.

$_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET. Good to use $_REQUEST on self refrential forms for validations.

Community
  • 1
  • 1
gewel
  • 411
  • 7
  • 7
  • 4
    +1 This is basically what I was taught. Not technical as other answers, but much easier to remember (`GET` from query string, `POST` from form submission). –  Apr 04 '14 at 14:58
18

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

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

The speed difference should be minimal either way.

Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
Daniel Bruce
  • 11,269
  • 4
  • 30
  • 28
8

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

Steven Schlansker
  • 37,580
  • 14
  • 81
  • 100
  • 7
    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 Dec 17 '09 at 22:11
  • 3
    You're right that nobody cares, but in my opinion using `$_REQUEST` is the wrong conclusion. See my answer. – Franz Dec 17 '09 at 22:13
  • 4
    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 Dec 17 '09 at 22:17
  • 6
    The claim that _REQUEST is more hygienic needs elaboration. –  Dec 17 '09 at 22:24
  • 2
    I'd recommend using GET if you want to user to be able to copy the URL and preform the same operation ie (it URL is visible like 'google.com/q=searchWord' while POST should be used to post data to a website that should only be inserted once, or lots of data is active and the user shouldn't be able to keep the url like inserting data in databases, logging in etc. – Dean Meehan Feb 18 '14 at 10:07
  • But `$_REQUEST` is insecure. I thought of making an XMLHttpRequest that sends it to a page where `$_REQUEST` is used. Since we would do this with GET most likely, it would take the user input with GET, not POST. This wouldn't be a problem if one person did it, but if someone1 tells someone2 to do it, someone1 can get someone2's password. – php.exe Oct 18 '15 at 05:38
7

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
  • 11,353
  • 8
  • 48
  • 70
6
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

Kristina
  • 15,859
  • 29
  • 111
  • 181
  • Not a bad solution at all. It takes care of the security flaws associated with `$_REQUEST` but still allows the same script to be accessed either way (in my case, the same script is used with different 'actions' and some times $_GET would be okay, but other times I need $_POST to hide/secure the data). – Xandor Jul 20 '19 at 15:25
4

$_GET retrieves variables from the querystring, or your URL.>

$_POST retrieves variables from a POST method, such as (generally) forms.

$_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET. Good to use $_REQUEST on self refrential forms for validations.

  • 2
    The overriding depends on [`request_order`](http://www.php.net/manual/en/ini.core.php#ini.request-order) and may contain cookie values too, which is why it's not a very reliable nor useful feature. – Ja͢ck Apr 21 '14 at 07:09
4

There are certain security concerns involved as a hacker can set a cookie that will override a $_POST or $_GET value. If you handle sensitive data, I would not recommend using $_REQUEST. – Xandor

you can't be used $_GET alternative of $_POST on some case.

When ??

  • when you want to upload a file.
  • when you don't won't to show a data in url.

GET also has limits on the amount of information to send. The limitation is about 2000 characters.

Other thing's there are few case when you can't retrieve a data using $_POST

When ?

  • when data is passed in URL.

For Rest Service

`GET` - Provides a read only access to a resource.

`PUT` - Used to create a new resource.

there is nothing be wrong to use $_REQUEST.

But the way to do that is to check $_SERVER['REQUEST_METHOD'] explicitly, not rely on $_POST being empty for a GET.

Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
  • 1
    Good advice on using `$_SERVER['REQUEST_METHOD']` to check if the script will be called with either one. But to say nothing is wrong with `$_REQUEST` is not 100% true. There are certain security concerns involved as a hacker can set a cookie that will override a $_POST or $_GET value. If you handle sensitive data, I would not recommend using `$_REQUEST`. – Xandor Jul 20 '19 at 16:14
  • I have added your comment in my answer it s make help thank you – Parth Chavda Jul 21 '19 at 09:11
1

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.

  • 1
    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 Dec 17 '09 at 22:12
1

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.

  • 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 Dec 17 '09 at 22:50
  • 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.) –  Dec 17 '09 at 23:16
1

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
1

I use this,

$request = (count($_REQUEST) > 1)?$_REQUEST:$_GET;

the statement validates if $_REQUEST has more than one parameter (the first parameter in $_REQUEST will be the request uri which can be used when needed, some PHP packages wont return $_GET so check if its more than 1 go for $_GET, By default, it will be $_POST.

Arun Panneerselvam
  • 2,263
  • 1
  • 17
  • 24
0

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
  • 11,218
  • 2
  • 35
  • 36
  • 3
    Please don't try to tell folk that there's anything more secure about POST than there is about GET. –  Dec 17 '09 at 22:23
  • 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 Dec 17 '09 at 22:29
  • 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. –  Dec 17 '09 at 22:39
0

It's ugly and I wouldn't recommended it as a final solution when pushing code live, but while building rest functions, it's sometimes handy to have a 'catch-all' parameter grabber:

public static function parseParams() {
    $params = array();
    switch($_SERVER['REQUEST_METHOD']) {
        case "PUT":
        case "DELETE":
            parse_str(file_get_contents('php://input'), $params);
            $GLOBALS["_{$_SERVER['REQUEST_METHOD']}"] = $params;
            break;
        case "GET":
            $params = $_GET;
            break;
        case "POST":
            $params = $_POST;
            break;
        default:
            $params = $_REQUEST;
            break;
    }
    return $params;
}

Someone creative could probably even add to it to handle command line parameters or whatever comes from your IDE. Once you decide what a given rest-function is doing, you can pick one appropriate for that given call to make sure you get what you need for the deploy version. This assumes 'REQUEST_METHOD' is set.

Scott
  • 7,983
  • 2
  • 26
  • 41