Información del cliente y servidor Web

Código fuente PHP

// 010-CodigoPHP.php
// Versión 1.0 10/12/2017 Juan Manuel Cueva Lovelle. Universidad de Oviedo
// Versión 1.1 08/12/2018 Escribe el contenido de $_SERVER

// Variables predefinidas - PHP proporciona una gran cantidad de variables predefinidas
// Algunas de las variables predefinidas son superglobales
// Las variables superglobales se pueden accedidas desde cualquier función, clase o archivo.
// Variables predefinidas superglobales:
// $GLOBALS
// $_SERVER
// $_REQUEST
// $_POST
// $_GET
// $_FILES
// $_ENV
// $_COOKIE
// $_SESSION

echo "<h3>Variables superglobales: GLOBALS</h3>";

$a = 27;
$b = 5;
 
function suma() {
    $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}
 
suma();
echo "<p>27 + 5 = " . $c. "</p>";

echo "<h3>Variables superglobales: _SERVER</h3>";

$nombreServidor = $_SERVER["SERVER_NAME"]; 
echo "<p>Su servidor es " . $nombreServidor. "</p>";

$ipServidor = $_SERVER["SERVER_ADDR"];
echo "<p>La dirección IP del servidor es " . $ipServidor. "</p>";

$softwareServidor =  $_SERVER["SERVER_SOFTWARE"];
echo "<p>El software del servidor es " . $softwareServidor. "</p>";

$ipCliente = $_SERVER["REMOTE_ADDR"];
echo "<p>La IP de la máquina cliente " . $ipCliente. "</p>";

$navegador = $_SERVER["HTTP_USER_AGENT"];
echo "<p>Información del USER AGENT del navegador: " . $navegador. "</p>"; 

$idiomaNavegador = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
echo "<p>Idioma del navegador: " . $idiomaNavegador. "</p>"; 

echo "<h3>Array asociativo con el contenido de _SERVER</h3>";
echo "<pre>";
print_r($_SERVER); //Escribe el contenido de $_SERVER
echo "</pre>";

Ejecución del código PHP

Variables superglobales: GLOBALS

27 + 5 = 32

Variables superglobales: _SERVER

Su servidor es di002.edv.uniovi.es

La dirección IP del servidor es 156.35.160.1

El software del servidor es Apache/2.4.18 (Ubuntu)

La IP de la máquina cliente 3.137.170.14

Información del USER AGENT del navegador: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)


Notice: Undefined index: HTTP_ACCEPT_LANGUAGE in /home/profesor/cueva/public_html/php/010-Server.php(21) : eval()'d code on line 48

Idioma del navegador:

Array asociativo con el contenido de _SERVER

Array
(
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
    [HTTP_ACCEPT_ENCODING] => gzip, br, zstd, deflate
    [HTTP_HOST] => di002.edv.uniovi.es
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    [SERVER_SIGNATURE] => 
Apache/2.4.18 (Ubuntu) Server at di002.edv.uniovi.es Port 80
[SERVER_SOFTWARE] => Apache/2.4.18 (Ubuntu) [SERVER_NAME] => di002.edv.uniovi.es [SERVER_ADDR] => 156.35.160.1 [SERVER_PORT] => 80 [REMOTE_ADDR] => 3.137.170.14 [DOCUMENT_ROOT] => /var/www/html [REQUEST_SCHEME] => http [CONTEXT_PREFIX] => /~cueva [CONTEXT_DOCUMENT_ROOT] => /home/profesor/cueva/public_html [SERVER_ADMIN] => webmaster@localhost [SCRIPT_FILENAME] => /home/profesor/cueva/public_html/php/010-Server.php [REMOTE_PORT] => 30337 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => GET [QUERY_STRING] => [REQUEST_URI] => /~cueva/php/010-Server.php [SCRIPT_NAME] => /~cueva/php/010-Server.php [PHP_SELF] => /~cueva/php/010-Server.php [REQUEST_TIME_FLOAT] => 1716124696.125 [REQUEST_TIME] => 1716124696 )