I inherited an XP machine with xitami/pro server running on it and installed PHP 5.2.17 because I thought I might need the VC6 version.
PHP works and the phpinfo shows as it should. When I do www.domain.com/test.php?x=y&z=test the $_GET is not being populated.
The $_REQUEST variable is not being populated either. If I post it in a form and post it, the $_POST is empty as is the $_REQUEST.
If I loop through the $_SERVER variables and display them on a get, the QUERY_STRING is populated with the get variables.
When I do a print_r on any of the variables, it is empty. I get: Array ( ) 1
I then upgraded to PHP 5.4 and the same thing.
What is the problem? I am at a loss and don't know what else to try.
I would suspect this problem arises when the server is configured wrong. Especially when the wrong SAPI is used (for example, I'm pretty sure $_GET/$_POST are not available when using the PHP CLI.
To see if this causes your issue, create a new php file, and insert the following
<?php
echo php_sapi_name();
?>
In case this returns CLI I'm pretty sure that causes your issues. Solve it by configuring your server to use the correct SAPI.
TL;DR:
I assume you're using C:\php\php.exe as your PHP interpreter. Try C:\php\php-cgi.exe instead.
Related
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.
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));
}
?>
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.
I have enabled APC in php.ini, and looking at phpinfo(), it all seems ok.
I also have apc.enable_cli=1 directive in php.ini.
I've stored several keys that start with raw_ prefix and they get stored (apc_store() returns true, apc_fetch() returns the value for that key.
All ok so far.
So I wanted to use APCIterator to fetch all keys that start with raw_, using this statement:
$iterator = new APCIterator("user", '/^raw_\.*/');
A key looks like this:
raw_2014-04-17 12:19:00_0.68206200 1397726355534f9c93a68649.18047787158329
It seems that if I print_r($iterator) or iterate it using foreach(), it is empty.
Any ideas what can be wrong?
Thank you!
Actually the problem was this: How can I get PHP to use the same APC cache when invoked on the CLI and the web?
I used CLI mode to fetch from APC and used store() via HTTP request. APC does not share data between Apache and CLI
According to http://www.php.net/manual/en/reserved.variables.globals.php :
An associative array containing references to all variables which are
currently defined in the global scope of the script.
So, following code must display that $GLOBALS var has _SERVER, _ENV (if it is enabled in variables_order in php.ini) and _REQUEST keys:
var_dump($GLOBALS);
The result is:
Under nginx + php-fpm: missing _SERVER, _ENV, _REQUEST
Under cli: missing _ENV, _REQUEST
Hmm.. perhaps there is smth in docs about this behavior? I've looked through every page for each variable:
_SERVER: http://www.php.net/manual/en/reserved.variables.server.php
_ENV: http://www.php.net/manual/en/reserved.variables.request.php
_REQUEST: http://www.php.net/manual/en/reserved.variables.request.php
And i have found no mentions about such behaviour. Why it works like that?
I have installed php using debian package from http://www.dotdeb.org/ repo (nothing was compiled manually)... Currently running with nginx + php5-fpm.
Is that a php bug?
I've created a bug on php.net website, and php team answered: https://bugs.php.net/bug.php?id=65223
Summary:
This is not a bug. super-globals (aka. auto globals) are not added
to symbol tables by default for performance reasons unless the parser
sees need. i.e.
<?php $_SERVER; print_r($GLOBALS); ?>
will list it. You can also control this using auto_globals_jit in
php.ini:
http://www.php.net/manual/en/ini.core.php#ini.auto-globals-jit
Thanks php team so answer so fast!
I am a beginner in php and am trying some very simple tests to get started.
I seem to be unable to get any values from $_GET.
This test.php
#!/usr/bin/php
<html>
<body><h1>GET test</h1><p>
<?php
print_r($_GET);
?>
</p></body></html>
produces the following when called with http://my.url/test.php?aValue=A&bValue=B
<html>
<body><h1>GET test</h1><p>
Array
(
)
</p></body></html>
I do not have write access to /etc/php.ini on the server, but check register_globals and it is off.
I have also tried using $_POST method, but this also doesn't work.
PHP version: PHP 5.1.6
The $_GET and $_POST variables are only available if track_vars is turned on.
As of PHP version 4.0.3 that is always automatically enabled.
Can you check your PHP version and also check the value of track_vars in php.ini?
It would also be helpful if you check phpinfo();
<?php
phpinfo();
?>
Check for
something called --enable-track-vars, which should be present.
_SERVER["argv"], should contain an array if you pass vars via a GET request.
also "Loaded Configuration File" should resolve to the file you think it is.
source: PHP: Description of core php.ini directives
I have scripts that use the $_SERVER["LOGON_USER"] which is obtained on my servers through IIS authentication settings. I want this same variable to contain my domain\username when running locally, or at least to have a way to set it when I fire up the PHP built-in server on localhost.
How can I configure PHP on my machine to obtain and fill this variable the same way it does when running through IIS?
PS: I have seen this question, but the answer addresses $_ENV, not $_SERVER.
This is a workaround, if anyone has a better/proper solution (i.e. enabling NTLM), please post it as an answer and I'll accept it.
I was able to fill that variable using a router script. According to the docs, this script is run at the start of each HTTP request, so I use it to set this variable when running locally.
Also in my case, my environment had these two variables set, USERDOMAIN and USERNAME, so I used them to form the LOGON_USER server variable.
routerCredentials.php
<?php
$_SERVER["LOGON_USER"] = getenv("USERDOMAIN") . "\\" . getenv("USERNAME");
return false; // serve the requested resource as-is.
To use it, you just have to point to that file when you start the PHP built-in server:
php -S localhost:8000 "c:\somepath\routerCredentials.php"