Why is $_SERVER['QUERY_STRING'] empty in Wordpress? - php

I made the GET request:
mysite.com/api/v1/articles?category=news
Then checked the $_SERVER on Wordpress:
$_SERVER['REQUEST_URI'] == '\/wp-json\/api\/v1\/articles?category=news'
$_SERVER['QUERY_STRING'] == ''
Why were the query arguments empty? $_GET was also empty.

Related

PHP: Variables sent via post are in the $_GET array and $_POST array is empty

I am running a PHP server with the following code on it.
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (isset($_POST["username"]) && isset($_POST["password"])) {
echo $_POST["username"];
http_response_code(200);
}
}
?>
I expected the script to return the username if both username and password are set in the post request.
However, the script returns nothing. When debugging with Postman I found out that the first if statement passes - I am sending a post request, but the $_POST array is empty. I found out that instead of the $_POST array, the $_REQUEST array, specifically the $_GET array contains my parameters.
The server REQUEST_METHOD is set to POST, yet the $_GET array contains my parameters.
So this code is working:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (isset($_GET["username"]) && isset($_GET["password"])) {
echo $_GET["username"];
http_response_code(200);
}
}
?>
Does anyone have an idea why this is happening? I have programmed with php some time ago, maybe I am missing something obvious.
The $_POST and $_GET superglobals aren't named after the HTTP methods (at least not directly).
They are named after the HTML form method attribute values and care about where the data is in the request.
$_GET is populated by data from the URL's query string
$_POST is populated by application/x-www-form-urlencoded and multipart/form-data encoded data in the request body
You must be making a POST request but putting the data in the URL's query string. (Note that data in the query string is usually logged, in plain text, in standard web server logs, so it is a really bad place to put passwords)
Use the Body tab and not the Params tab to enter your data. Make sure you pick form-data or x-www-form-urlencoded.

Predefining $_GET parameter on open of index.php

Is there a way to set a $_GET parameter with certain value on open of "index.php"
For example: the url being = ".../index.php?somePara=value"
I tried using :
header("Location: index.php?page=1");
exit();
But I get an error of
"localhost redirected you too many times."
Edit:
Thanks, Qirel, that works, I need it because i have variables that are attached to $_GET parameter, so instead of setting them to NULL and then passing value to them after a if($_GET) check -> so that they wont get an error of undefined, I was wondering how pass value on open of the file.
The problem is either that you do not have a condition, or you have, but it is always true in this use-case.
Your question mentions the condition:
Is there a way to set a $_GET parameter with certain value on open of
"index.php"
but your code does not contain that:
header("Location: index.php?page=1");
exit();
So you can wrap this into a condition:
if (isset($_GET["certain"]) && ($_GET["certain"] === "value")) {
header("Location: index.php?page=1");
exit();
}
Note that the string parameter of header does not contain certain, since that would result in an infinite loop. You can also check in the if whether there is a page parameter if you intend to include certain there.
I have read the comment which suggested to set the corresponding key of $_GET to the expected value instead of a redirect, but that is not always an option, since you might want to beautify a URL or even to redirect to another site's page.
If $_GET parameters are used in my code, I always set a default value for these variables since you are never sure if it is set.
$name = isset($_GET['name']) ? $_GET['name'] : 'Guest';
// This is the shorter version for
if (isset($_GET['name'])) {
$name = $_GET['name'];
} else {
$name = 'Guest';
}
Your method of solving your problem will get you into an infinite loop. You could however do:
if (!isset($_GET['page'])) {
header("Location: index.php?page=1");
exit;
}

Using $_GET with brackers in URL

I have the following code...
<?php
if ($_GET['custom_fields%5Bcartredirectmb%5D'] == 'test')
{ header("Location: http://www.google.com"); exit(); }
?>
I just need this code to get the parameter 'custom_fields[cartredirectmb]' and redirect based on its value.
It looks like it's hung up on the brackets in the parameter. I've tried using urlencode, but I'm not getting anywhere. I assume there's a simple answer to this get working.
Feedback? Thanks!
PHP automatically expands array expressions in the URL into the $_GET variable.
The following variable in the URL,
?custom_fields[cartredirectmb]=test
can be accessed through the cartredirectmb key, in the custom_fields array in the URL.
$_GET['custom_fields']['cartredirectmb']

$_GET reads url as string and not as an array

My $_GET is reading the URL as a string and does not separate different GET variables.
My URL is:
www.mysite.com/index.php?cat=archive?page=2
Using $_GET I'd like to use the variables $cat and $page.
echo $_GET['cat'];
Returns: archive?page=2.
echo $_GET['page'];
Returns: nothing.
What am I doing wrong here? Should I manually separate the URL into variables?
the '?' is only used once in the url. you should use & to separate each variable.
your URL should be www.mysite.com/index.php?cat=archive&page=2
and then you can echo on cat and page
url in not correct form where you are made it
www.mysite.com/index.php?cat=archive?page=2
correct it with
www.mysite.com/index.php?cat=archive&page=2

Get full page URL with PHP

I am trying to get the entire page URL as a string in PHP - so, if the requested URL is ./foo.php?arg1=test&arg2=test2, I get "./foo.php?arg1=test&arg2=test2".
I know that I can get the ./foo.php part from $_SERVER and the variables from $_GET, but I was wondering if there's an easy way to do it in just one fell swoop.
TIA.
$url = (isset($_SERVER['HTTPS']) == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
This should return the full url based on what was typed in the address bar.
If I open the following URL in my browser :
http://tests/temp/temp.php?a=145&b=glop
The following piece of code :
var_dump($_SERVER['REQUEST_URI']);
Gives me :
string '/temp/temp.php?a=145&b=glop' (length=27)
So, $_SERVER['REQUEST_URI'] might be what you are looking for...
$_SERVER['REQUEST_URI']
http://au2.php.net/manual/en/reserved.variables.server.php

Categories