Apache removes all $_REQUEST, $_POST, etc
PuffPastry opened this issue · 6 comments
Hi,
I've setup an Api with your script. My Problem is that I can only access $_POST data via file_get_contents( 'php://input' )
. The downside is that I cannot write a middleware for authentication, where I would need to access headers and cookies.
That data appears to be lost during the rewrite to index.php
Would you happen to have encountered this before? I see you reference $_POST in your samples of the route implementations. Does that mean you have overcome the issue or never faced it? What could I do to pass the contents of $_POST and $_COOKIES?
Any help is much appreciated
hey @PuffPastry,
never had this problem or heard about it.
What is the output if you try this script:
<?PHP
print_r($_GET);
print_r($_POST);
print_r($_COOKIE);
Are these arrays completely empty? What is the output with cookies send to the server or with request strings like ?foo=bar
I played around with all that now, here is the output:
print_r( $_GET );
print_r( $_POST );
print_r( $_COOKIE );
print_r( $_REQUEST );
================
Output:
Array ( [test] => true )
Array ( )
Array ( [Cookie_1] => value )
Array ( [test] => true )
So it apears it is just the $_POST that gets dropped for me.. given that, the issue becomes a bit moot.
For Reference, I am running wamp with Apache 2.4.41 with PHP 8.1.1 ( I also tried php 7.3, 7.2 and 7.1)
whether you execute and call them directly or by using ajax, All HTTP requests work fine for me, have you tried downgrading your PHP version, you could try 7.3 or 7.4,
Maybe you not doing a correct POST request?
Check this out: https://www.php.net/manual/en/reserved.variables.post.php
So what is inside your echo $_SERVER['REQUEST_METHOD'];
variable?
Also check the print_r(apache_request_headers());
output.
The request must be of type application/x-www-form-urlencoded or multipart/form-data to fill the $_POST superglobal.
I also have another idea: Rename your .htaccess file and call a php file directly without mod_rewrite. Then check if the $_POST array gets filled. If this is the case you are correct and something must be wrong with the rewrite. In this case try to add a 'P' to the rule: https://stackoverflow.com/questions/358263/is-it-possible-to-redirect-post-data
Thanks all for the effort. @steampixel the output of print_r(apache_request_headers()); is pretty standard, $_SERVER also appears to be untouched, except that the POST data is also not present there.
I've use file_get_contents( 'php://input' )
as a workaround, it's working fine that way and I have access to all variables and cookies.
Thanks again for providing this simple little Gem, makes my barebones api a lot cleaner.