filter_input(INPUT_SERVER,'DOCUMENT_ROOT'); returns null

1.3k Views Asked by At

My site is hosted on a shared host. I’m trying to setup a new development environment (when I say new, I mean that it doesn’t look like any of my previous development environment which ranges from things like a single Notepad++ to “easy” server solutions like WAMP/XAMPP … yet I’m a C#/.NET development so php is more something I casually use at home ...)

My new environment so far has:

  • Windows 10 (because that’s what I use at home)
  • PhpStorm.
  • Php 7.3.3 VC15 x64 Thread Safe unzipped somewhere on my my hard drive.
  • php.exe refered as interpreter in PhpStorm
  • php_xdebug-2.7.0-7.3-vc15-x86_64.dll set has debugged in php.ini

In my php code I have

filter_input(INPUT_SERVER,'DOCUMENT_ROOT');

It returns null (or something that is empty).

When I paste it in the terminal is says:

'filter_input' is not recognized as an internal or external command, operable program or batch file.

Well, maybe that’s the type of terminal I’m expecting but the fact that it returns null is a problem.

What should I change so that it returns the same value as in my online environment?

I have tried solutions from PHP filter_input(INPUT_SERVER, 'REQUEST_METHOD') returns null? including adding variables_order = "GPCSE" to php.ini, but it didn’t solve the problem.

Writing filter_var(getenv('DOCUMENT_ROOT')); instead but it didn’t solve the problem ...

I want to code for my online environment not to adapt my code to my development environment.


Further test 1

Running code

var_dump(realpath(dirname(__FILE__).'/../'));

if($_SERVER['DOCUMENT_ROOT'] === '')
    $_SERVER['DOCUMENT_ROOT'] = realpath(dirname(__FILE__).'/../');

var_dump($_SERVER['DOCUMENT_ROOT']);

var_dump(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT'));

1st var_dump shows correct value. 2nd var_dump shows correct value. 3rd var_dump shows string(0) "". I'm not quite sure how $_SERVER and INPUT_SERVER are related though.

2

There are 2 best solutions below

7
Jim Grant On

This will see if your server can parse the correct information from INPUT_SERVER and INPUT_ENV. If the first var_dump does not show the "DOCUMENT_ROOT" variable, then the filter_input function won't either.

<?php
var_dump($_SERVER);
foreach ( array_keys($_SERVER) as $b ) {
    var_dump($b, filter_input(INPUT_SERVER, $b));
}
echo '<hr>';
var_dump($_ENV);
foreach ( array_keys($_ENV) as $b ) {
    var_dump($b, filter_input(INPUT_ENV, $b));
}
?>
0
TTT On

Here some way I found:

Apparently, when you click on the debug button, PhpStorm does not run PHP scripts as if they were accessed via a server, hence some values like DOCUMENT_ROOT are not filled.

However, when accessing to http://localhost:63342/[path]/[filename]  , DOCUMENT_ROOT indeed contains the expected value.

Note: using localhost (or other names) may require editing \Windows\System32\Drivers\Etc\hosts file.

I have also set a "server" in PhpStorm (Settings > Languages & Frameworks > PHP > Servers ) on localhost:80 but this one seems ineffective and unrelated, you probably don't need it, not sure.

EDIT: I just realised this is actually only a partial way around because if I use this, debug seem ineffective, breakpoints are not triggered.