php `$_SERVER['PHP_SELF']` 为空

Posted

技术标签:

【中文标题】php `$_SERVER[\'PHP_SELF\']` 为空【英文标题】:php `$_SERVER['PHP_SELF']` is emptyphp `$_SERVER['PHP_SELF']` 为空 【发布时间】:2019-07-03 11:48:28 【问题描述】:

我的 nginxphp 配置有问题。由于某种原因,$_SERVER['PHP_SELF'] 为空。

我在跑步:

Ubuntu 18.04 nginx(nginx版本:nginx/1.14.0(Ubuntu)) php (PHP 7.2.10-0ubuntu0.18.04.1)

我不知道可能导致此问题的原因。我在网上看了很多,但找不到任何解决方案。

只是想看看是否缺少更多信息,我检查了我从 print_r($_SERVER); 得到的信息,这就是我得到的信息(已审查的私人信息...):

数组 ( [USER] => www-data [HOME] => /var/www [HTTP_COOKIE] => 审查 [HTTP_ACCEPT_LANGUAGE] => en-GB,en;q=0.9, he-IL;q=0.8,he;q=0.7,en-US;q=0.6,ru;q=0.5 [HTTP_ACCEPT_ENCODING] => gzip, deflate, br [HTTP_ACCEPT] => text/html,application/xhtml+ xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 [HTTP_USER_AGENT] => 审查 [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [ HTTP_CACHE_CONTROL] => max-age=0 [HTTP_CONNECTION] => keep-alive [HTTP_HOST] => cumta.morhaviv.com [SCRIPT_FILENAME] => /var/www/morhaviv.com/public_html/cumta/css/test.php [REDIRECT_STATUS] => 200 [SERVER_NAME] => www.cumta.morhaviv.com [SERVER_PORT] => 443 [SERVER_ADDR] => 153.92.209.235 [REMOTE_PORT] => 22964 [REMOTE_ADDR] => 176.231.2.86 [SERVER_SOFTWARE] = > nginx/1.14.0 [GATEWAY_INTERFACE] => CGI/1.1 [HTTPS] => on [REQUEST_SCHEME] => https [SERVER_PROTOCOL] => HTTP/1.1 [DOCUMENT_ROOT] => /var/www/morhaviv.com/public_html/ cumta [DOCUMENT_URI] => /css/test.php [REQUEST_URI] => /css/test.php [SCRIPT_NAME] => /css/test.php [CONTENT_LENGTH] => [CONTENT_TYPE] => [REQUEST_METHOD] => GET [QUERY_STRING] => [PATH_INFO] => [FCGI_ROLE] => RESPONDER [PHP_SELF] => [REQUEST_TIME_FLOAT ] => 1549710420.5126 [REQUEST_TIME] => 1549710420)

我的 nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events 
    worker_connections 768;
    # multi_accept on;


http 
    include        fastcgi_params; 
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

我知道这个问题可能缺少一些需要的更多信息,但我真的不知道还有什么重要的,所以请发表评论,我会添加缺失的信息。

感谢您的帮助!

【问题讨论】:

检查谷歌。无效的 php-cgi 配置。 @emix 我做了,但我没有找到任何解决我的问题的方法... 另外,如果你打算使用 PHP_SELF。避免使用它。或者用htmlentities( )围起来,因为它容易受到XSS注入。 【参考方案1】:

fix_pathinfo 设置为 1 是一个潜在的漏洞。而是将此行添加到 nginx php_fpm 部分:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

只需手动分配全局变量:

$_SERVER['PHP_SELF'] = !empty($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '/'.basename($_SERVER['SCRIPT_FILENAME']);

More info

【讨论】:

【参考方案2】:

一些用php安装nginx服务器的教程,坚持把php.ini文件的参数cgi.fix_pathinfo改成0。

所以我的解决方案在php.ini:

cgi.fix_pathinfo = 0 

到默认值:

cgi.fix_pathinfo = 1

基本上就是这样。

感谢 kenzotenma 对他的回答的评论,找到了解决方案,链接:https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

【讨论】:

我想这不是安全的解决方案 - security.stackexchange.com/questions/177354/… 使用 security.limit_extensions 或 nginx try_files/if,这是安全的【参考方案3】:

确保在 nginx 配置文件中包含这一行:

include fastcgi_params

除此之外,我认为它没有任何不工作的理由。

希望对你有帮助

【讨论】:

我需要将它包含在 fastcgi 配置文件中,还是包含在站点可用目录中的站点配置文件中? @morha13 主配置文件 - nginx.conf。可能在/etc/nginx/nginx.conf,具体取决于您使用的操作系统 感谢您的帮助!我尝试添加它,但它似乎仍然不起作用。我尝试将它添加到 nginx.conf 文件中,这是我实现它的方式:pastebin.com/KbxDuQZR 尝试关注这个:nginx.com/resources/wiki/start/topics/examples/phpfcgi 也许你会弄清楚你需要什么。解决后让我知道 好的,我试试,谢谢!如果我想通了,我会告诉你的

以上是关于php `$_SERVER['PHP_SELF']` 为空的主要内容,如果未能解决你的问题,请参考以下文章

$_SERVER变量 以及 PHP 使用 $_SERVER['PHP_SELF'] 获取当前页面地址及其安全性问题

php` $ _SERVER ['PHP_SELF']`是空的

如何避免 $_SERVER["PHP_SELF"] 被利用?

PHP_SELF SCRIPT_NAME REQUEST_URI区别

__FILE__ $_SERVER['PHP_SELF'] $_SERVER['SCRIPT_NAME'] $_SERVER['SCRIPT_FILEN

如何利用$_SERVER["PHP_SELF"]变量植入script代码?