Among $_REQUEST, $_GET and $_POST which one is the fastest?
$_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.
Why is the GET method faster than POST in HTTP?
It's not much about speed. There are plenty of cases where POST is more applicable. For example, search engines will index GET URLs and browsers can bookmark them and make them show up in history. As a result, if you take actions like modifying a DB based on a GET request, it might be harmful as some bots might also traverse the URL.
The other case can be security issue. If you post credentials using GET, it'll get listed in browser history and server log files.
what $_REQUEST will return when same values sent by GET and POST?
The actual order is determined in the "request_order" setting in the PHP.ini file
; This directive determines which super global data (G,P,C,E & S) should
; be registered into the super global array REQUEST. If so, it also determines
; the order in which that data is registered. The values for this directive are
; specified in the same manner as the variables_order directive, EXCEPT one.
; Leaving this value empty will cause PHP to use the value set in the
; variables_order directive. It does not mean it will leave the super globals
; array REQUEST empty.
; Default Value: None
; Development Value: "GP"
; Production Value: "GP"
; http://php.net/request-order
request_order = "GP"
Usually the default setting is Get then Post. In this case you supply the id parameter as get AND as a post parameter. This means the $_REQUEST is populated with the $_GET first, then $_POST. Meaning $_REQUEST will reflect $_POST.
Unable to get posted data by $_GET, $_POST & $_REQUEST in Angularjs
To go along with my comment about a possibility of a bad setting in php.ini, this article(dead link) (linked through this one, from my comment) also offered this quote:
If the Content-Type is empty or not recognized in the HTTP message then the PHP $_POST array is empty. Not sure if this is a bug or is by design…
The symptoms being the same, (empty $_POST
but data available through php://input
), this brings up the number of possible reasons this error is occurring to two:
- Syntax error in php.ini
post_max_size
setting - Bad value or empty Content-Type header being sent from the front-end
If your setting for post_max_size
is correct, then it could be that angular is removing the Content-Type
header for the post. According to this question and this question, angular will remove the Content-Type header if there is no payload or an empty payload going along with any request, which I believe is your issue.
Try adding this in your SearchCtrl
:
function SearchCtrl($scope, $http) {
$scope.url = 'search.php'; // The url of our search
$scope.test = {test:'test'}; // Add me
// ...
I suspect your $_POST
array will become filled, since there will now be a Content-Type header being sent along with the request, and actual request data being sent. If that works, it means that your test
input isn't binding correctly to the scope, or you're submitting empty data. Try adding a guard clause before the HTTP Request:
if($scope.test) {
$http.post($scope.url, { "data" : $scope.test})
// ...
In which condition we use $_REQUEST variable instead of $_GET or $_POST variable
The short answer is: Always use $_GET
or $_POST
. $_REQUEST
is synonymous with either but detracts from being a diligent, strict coder.
PHP $_REQUEST Variable
It depends on the request_order
configuration directive (quoting) :
This directive describes the order in which PHP registers GET, POST
and Cookie variables into the_REQUEST
array.
Also take a look at variables_order
: some additional explanations (like the letters that can be used) are there -- and it also affects $_REQUEST
.
Azure Function App - PHP $_GET / $_POST / $_REQUEST
You can access request parameters/headers by referencing REQ_QUERY_<NAME>
/REQ_HEADERS_<NAME>
environment variables. For example:
<?php
/* request body */
$body = file_get_contents(getenv('req'));
$body = rtrim($body, "\n\r");
fwrite(STDOUT, "PHP script processed request body '$body'\r\n");
/* access query parameter 'test' */
$test = getenv('req_query_test');
fwrite(STDOUT, "Parameter Test='$test'\r\n");
/* access header 'test' */
$test = getenv('req_headers_test');
fwrite(STDOUT, "Header Test='$test'\r\n");
?>
Similarly, if your function has route parameters, they can be accessed via REQ_PARAMS_<NAME>
environment variables.
$_REQUEST works, but $_POST doesn't -
If you want to send it as $_POST
, change this line:
method: 'POST'
to this:
type: 'POST'
According to docs, thats the way of setting it.
type (default:
'GET'
)
Type: String
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.
Related Topics
Stacking Multiple Ternary Operators in PHP
Send Email With PHP from HTML Form on Submit With the Same Script
How to Generate a Random, Unique, Alphanumeric String
Extract a Single (Unsigned) Integer from a String
Escaping Quotation Marks in PHP
How to Get Innerhtml of Domnode
How to Search by Key=≫Value in a Multidimensional Array in PHP
How to Get Body of a Post in PHP
Seo Friendly Url Results in CSS Img and Js Not Working
Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (Codeigniter + Xml-Rpc)
How to Bind an Array of Strings With MySQLi Prepared Statement
Accessing @Attribute from Simplexml
Allowed Memory Size of 33554432 Bytes Exhausted (Tried to Allocate 43148176 Bytes) in PHP