I've noticed weird issue which causing the redirect to home page while running batch script (such as indexing the site).
I've tracked the problem to request_path()
which returns empty string despite I'm being on /batch?op=start&id=10916
page, as result I'm redirected to the home page which is not expected.
I'm using PHP built-in server which is probably the reason of such problem (ran by: php -S localhost:1234
).
In request_path() I've tracked the problem to the following code which "thinks" this is a home page request:
// If the path equals the script filename, either because 'index.php' was
// explicitly provided in the URL, or because the server added it to
// $_SERVER['REQUEST_URI'] even when it wasn't provided in the URL (some
// versions of Microsoft IIS do this), the front page should be served.
if ($path == basename($_SERVER['PHP_SELF'])) {
$path = '';
}
These are my results of some testing the values by the following line:
var_dump('PHP_SELF = ' . $_SERVER['PHP_SELF'], 'basename = ' . basename($_SERVER['PHP_SELF']), '$path = ' . $path); exit;
For example:
Page: /batch?op=start&id=10916
string 'PHP_SELF = /index.php/batch' (length=27)
string 'basename = batch' (length=16)
string '$path = batch' (length=13)
Problem: Since PHP_SELF basename equals to batch, Drupal thinks this is the file, therefore removing the correct value of $path
Page: update.php
string 'PHP_SELF = /update.php' (length=22)
string 'basename = update.php' (length=21)
string '$path = update.php' (length=18)
This is correct.
Page: /user
string 'PHP_SELF = /index.php/user' (length=26)
string 'basename = user' (length=15)
string '$path = user' (length=12)
This is not correct and will result in homepage page.
Page: /admin/reports
string 'PHP_SELF = /index.php/admin/reports' (length=35)
string 'basename = reports' (length=18)
string '$path = admin/reports' (length=21)
This won't redirect to homepage.
I think the fix should be similar as proposed in #223496: Use SCRIPT_FILENAME instead of PHP_SELF 7 years ago, so to use $_SERVER['SCRIPT_FILENAME'] instead.
So basename of SCRIPT_FILENAME will return: index.php, not batch, in case of batch case.
Comment | File | Size | Author |
---|---|---|---|
#6 | Drupal_redirect_on_batch_2691107.patch | 568 bytes | AstonVictor |
bootstrap.inc-request_path.patch | 568 bytes | kenorb | |
Comments
Comment #3
geek-merlinNginx? That may be #2490870: Use request_uri() instead of $_SERVER['REQUEST_URI'] everywhere
Comment #4
kenorb CreditAttribution: kenorb commentedComment #5
kenorb CreditAttribution: kenorb commentedNot nginx, it was by using built-in PHP server.
Comment #6
AstonVictor CreditAttribution: AstonVictor as a volunteer and at DevBranch commented