Consultar todos los valores de la variable superglobal $_SERVER

Código PHP

PHP – Server – Consultar valores $_SERVER

Función que devuelve un array con todos los valores que contiene la variable superglobal $_SERVER. Dependiendo de como se consulta esta variable, si es desde el prompt o desde una llamada http, tiene diferentes valores.

/**
 * Class HelperServer
 */
abstract class HelperServer
{
    /**
     * Devuelve un array con todos los valores 
     * de la variable superglobal $_SERVER
     * NOTA: filter_input para las opciones INPUT_SERVER y 
     * INPUT_ENV no funcionan para FASTCGI
     *
     * @return array
     */
    public static function getAllValues( )
    {
        $ret = [ ];
        foreach ( array_keys( $_SERVER ) as $key) {
            $exists = filter_has_var( INPUT_SERVER, $key );
            if ($exists) {
                $value = filter_input( INPUT_SERVER, $key );
            } else {
                $value = filter_var( $_SERVER[ $key ], 
                                     FILTER_DEFAULT, 
                                     null );
            }
            $ret[ $key ] = $value;
        }

        return $ret;
    }
}

Ejemplo

$array_server = HelperServer::getAllValues();
print_r($array_server);

/*
Ejecución modo comando desde el prompt
Array
(
    [ALLUSERSPROFILE] => C:\ProgramData
    [APPDATA] => C:\Users\MyUserName\AppData\Roaming
    [CommonProgramFiles] => C:\Program Files (x86)\Common Files
    [CommonProgramFiles(x86)] => C:\Program Files (x86)\Common Files
    [CommonProgramW6432] => C:\Program Files\Common Files
    [COMPUTERNAME] => NAME_OF_COMPUTER
    [ComSpec] => C:\WINDOWS\system32\cmd.exe
    [DriverData] => C:\Windows\System32\Drivers\DriverData
    [HOMEDRIVE] => C:
    [HOMEPATH] => \Users\UserName
    [LOCALAPPDATA] => C:\Users\MyUserName\AppData\Local
    [LOGONSERVER] => \\NAME_OF_COMPUTER
    [NUMBER_OF_PROCESSORS] => 8
    [OneDrive] => C:\Users\MyUserName\OneDrive
    [OnlineServices] => Online Services
    [OS] => Windows_NT
    [Path] => C:\Program Files (x86)\Common Files\Oracle\Java\javapath;...
    [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    [platformcode] => 1M
    [PROCESSOR_ARCHITECTURE] => x86
    [PROCESSOR_ARCHITEW6432] => AMD64
    [PROCESSOR_IDENTIFIER] => Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
    [PROCESSOR_LEVEL] => 6
    [PROCESSOR_REVISION] => 9e09
    [ProgramData] => C:\ProgramData
    [ProgramFiles] => C:\Program Files (x86)
    [ProgramFiles(x86)] => C:\Program Files (x86)
    [ProgramW6432] => C:\Program Files
    [PSModulePath] => C:\Program Files\WindowsPowerShell\Modules;...
    [PUBLIC] => C:\Users\Public
    [RegionCode] => EMEA
    [SESSIONNAME] => Console
    [SystemDrive] => C:
    [SystemRoot] => C:\WINDOWS
    [TEMP] => C:\Users\DATADE~1\AppData\Local\Temp
    [TMP] => C:\Users\DATADE~1\AppData\Local\Temp
    [USERDOMAIN] => NAME_OF_SERVER
    [USERDOMAIN_ROAMINGPROFILE] => NAME_OF_SERVER
    [USERNAME] => UserName
    [USERPROFILE] => C:\Users\MyUserName
    [windir] => C:\WINDOWS
    [XDEBUG_CONFIG] => idekey=18571
    [PHP_SELF] => Z:\www\Php\getAllValues.php
    [SCRIPT_NAME] => Z:\www\Php\getAllValues.php
    [SCRIPT_FILENAME] => Z:\www\Php\getAllValues.php
    [PATH_TRANSLATED] => Z:\www\Php\getAllValues.php
    [DOCUMENT_ROOT] =>
    [REQUEST_TIME_FLOAT] => 1590322919.7879
    [REQUEST_TIME] => 1590322919
    [argv] =>
    [argc] => 1
)

Ejecución desde el navegador con Apache
Array
(
    [MIBDIRS] => C:/xampp/php/extras/mibs
    [MYSQL_HOME] => \xampp\mysql\bin
    [OPENSSL_CONF] => C:/xampp/apache/bin/openssl.cnf
    [PHP_PEAR_SYSCONF_DIR] => \xampp\php
    [PHPRC] => \xampp\php
    [TMP] => \xampp\tmp
    [HTTP_HOST] =>localhost
    [HTTP_CONNECTION] => keep-alive
    [HTTP_PRAGMA] => no-cache
    [HTTP_CACHE_CONTROL] => no-cache
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;...
    [HTTP_SEC_FETCH_SITE] => none
    [HTTP_SEC_FETCH_MODE] => navigate
    [HTTP_SEC_FETCH_USER] => ?1
    [HTTP_SEC_FETCH_DEST] => document
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
    [HTTP_ACCEPT_LANGUAGE] => es,en;q=0.9,ca;q=0.8,fr;q=0.7,ja;q=0.6
    [HTTP_COOKIE] => XDEBUG_SESSION=15629; PHPSESSID=g9re05r5mpu6khu2nkei31j9ln; tzo=120
    [PATH] => C:\Program Files (x86)\Common Files\Oracle\Java\javapath;...
    [SystemRoot] => C:\WINDOWS
    [COMSPEC] => C:\WINDOWS\system32\cmd.exe
    [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    [WINDIR] => C:\WINDOWS
    [SERVER_SIGNATURE] => Apache/2.4.33 (Win32) OpenSSL/1.1.0g PHP/7.2.4 Server at domain.localhost Port 80
    [SERVER_SOFTWARE] => Apache/2.4.33 (Win32) OpenSSL/1.1.0g PHP/7.2.4
    [SERVER_NAME] => domain.localhost
    [SERVER_ADDR] => ::1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => ::1
    [DOCUMENT_ROOT] => Z:/www/documentroot
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] =>
    [CONTEXT_DOCUMENT_ROOT] => Z:/www/documentroot
    [SERVER_ADMIN] => postmaster@localhost
    [SCRIPT_FILENAME] => Z:/www/documentroot/index.php
    [REMOTE_PORT] => 38466
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => XDEBUG_SESSION_START=15629
    [REQUEST_URI] => /?XDEBUG_SESSION_START=15629
    [SCRIPT_NAME] => /index.php
    [PHP_SELF] => /index.php
    [REQUEST_TIME_FLOAT] => 1590323400.688
    [REQUEST_TIME] => 1590323400
)
 */