PHP's filter_input() strips $_SERVER data on external host, but works on localhost

耗尽温柔 提交于 2019-12-19 08:40:36

问题


I am running an apache2 server where I do my localhost testing (PHP 5.5), but my hosting provider has PHP 5.3. I don't know whether that is an issue, but I mention it just in case.

My problem is this: I am seeking to note a couple of $_SERVER variables, but the filter_input() function returns false for some reason, but only on my host server. It works fine on localhost.

Echoing verifies the expected output:

echo $_SERVER['HTTP_USER_AGENT'];
echo $_SERVER['REMOTE_ADDR'];

Returns as expected:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36
90.10.160.140    (Not the actual address)

But when I use the filter (even without a filter/options specified!), both are blank:

filter_input(INPUT_SERVER,'HTTP_USER_AGENT',FILTER_SANITIZE_ENCODED,FILTER_FLAG_STRIP_LOW);
filter_input(INPUT_SERVER,'REMOTE_ADDR',FILTER_VALIDATE_IP);
filter_input(INPUT_SERVER,'HTTP_USER_AGENT');
filter_input(INPUT_SERVER,'REMOTE_ADDR');

All of the above return an empty string/FALSE when executed on my hosting provider, yet work as expected on my localhost.

I'm guessing there is some config parameter set differently on the host. Any idea which one? Or what else might be the matter? Thanks.


回答1:


I had the same problem, I won't be able to tell you why it returns an empty string/FALSE but for me using INPUT_ENV instead of INPUT_SERVER did work and returns the same IP-address as $_SERVER['REMOTE_ADDR']. Same goes for HTTP_USER_AGENT. Short example:

$REMOTE_ADDR = filter_input(INPUT_ENV, 'REMOTE_ADDR', FILTER_VALIDATE_IP);



回答2:


My guess would be something related to this bug: https://github.com/wp-stream/stream/issues/254

Try to use the bugfix on your code as a workaround



来源:https://stackoverflow.com/questions/25316085/phps-filter-input-strips-server-data-on-external-host-but-works-on-localho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!