NOTA: codice commentato il 2022.01.14 perché già inserito tramite Google SiteKit // exclude auto ads from privacy & cookie policy $currentSlug = basename(get_permalink()); if ($currentSlug != "privacy" && $currentSlug != "privacy-info" && $currentSlug != "cookie" && $currentSlug != "cookie-info") { ?> */ ?> function EzConsentCallback(consent) { // necessary, preferences, statistics, marketing (true or false) if(consent.marketing) { // TODO: do marketing stuff here - like insert ads. } } */ ?> */ ?>
Domenica 6 dicembre 2026Online dal 2014Articoli, tutorial, soluzioni software e progetti open source
PHP

Show My IP: visualizzare l'indirizzo IP e lo User-Agent dell'utente corrente in modo "sicuro"

Visualizza il tuo indirizzo IP, lo User-Agent e altre informazioni relative al client utilizzato velocemente e senza Ads invasivi.

In estrema sintesi: il tuo indirizzo IP è visibile qui. Per visualizzare anche User-Agent, Proxy-Forwarded IP e Client IP visita questa pagina. Se ti interessano maggiori informazioni sull'argomento, continua a leggere.

Non è infrequente, sviluppando siti e servizi per il web, avere bisogno di conoscere in modo oggettivo l'indirizzo IP pubblico con cui la macchina che stiamo configurando o utilizzando risulta connessa a Internet. Per quanto esistano innumerevoli modi per ottenere questa informazione in modo empirico - ipconfig , interfaccia admin del router et. al. - niente è immediato e inequivocabile come un servizio di tipo "ShowMyIP". Non è un caso se ne esistono migliaia, tutti più o meno analoghi. Non c'è niente da vergognarsi nell'utilizzarli, specialmente se l'informazione ci serve immediatamente.

Personalmente, ci sono ben tre cose che non sopporto nel 99.9% di quei siti:

  • lo straripante quantitativo di Ads, che non di rado impediscono di ottenere l'informazione cercata mostrando (appositamente) decine di IP che non c'entrano nulla per confondere l'incauto utente.
  • l'assoluta incapacità di mostrare l'indirizzo IP nel posto giusto, ovvero nella porzione above-the-fold della pagina: possibile che sia così difficile, in un servizio fatto apposta? Ovviamente si, visto che il tentativo è quello di tenerci lì a vedere gli Ads.
  • la cronica mancanza di informazioni sugli header di eventuali Proxy e/o sul client in generale (user-agent, custom headers, client-ip, etc.). Per carità, si tratta di informazioni aggiuntive rispetto allo scopo della pagina, ma in un servizio dedicato allo sniffing dell'indirizzo IP del chiamante potrebbe essere un modo utile per riempire il resto della pagina - magari al posto di qualche altra Ad unit.

Per questo motivo ho deciso di inaugurare la mia pagina ShowMyIP personale, che contiene le seguenti informazioni:

  • Indirizzo IP utilizzato, con un paio di righe che spiegano come è stato ottenuto.
  • Indirizzo IP inoltrato da un eventuale Proxy (se presente), con un paio di righe che spiegano da dove proviene e quanto è affidabile.
  • Indirizzo IP indicato dal client (se presente), con un paio di righe che spiegano come viene inoltrato e quanto è affidabile.
  • User-Agent del browser, con un paio di informazioni a corredo.

La pagina può essere consultata cliccando qui: in coda all'articolo, per i più interessati, allego una copia del codice sorgente PHP utilizzato, completo di commenti.

Se vi vengono in mente altre informazioni utili fatemelo sapere e provvederò ad aggiungerle!

Felice sniffing!

*/ ?>
PHP
function PrintUserExtendedIPInfo() {
 
    $remote_label="Here's your current IP Address:";
    $remote_info="This is the IP Address sent by the Web Server: usually reliable, it could not show the real IP address of your machine if you're behind a Proxy service: it's also kinda difficult to spoof/hack. It has been obtained by looking at the <strong>REMOTE_ADDR</strong> Server Variable.";
    $client_label="IP Address sent by your Browser:";
    $client_info="This is the IP Address sent by your browser client: if set, it usually shows the real IP address of your machine. It can be easily altered/spoofed/hacked by altering your browser client's settings. It is obtained by looking at the <strong>HTTP_CLIENT_IP</strong> Server Variable.";
    $forward_label="IP Address sent by your Proxy:";
    $forward_info="This is the IP Address sent by the Proxy server you're using to connect to this page: if present, it usually shows the real IP address of your machine - providing your Proxy is properly set to pass it out. It can be easily altered/spoofed/hacked by altering your browser client's settings, manipulating your Proxy or adding another Proxy to the chain. It is obtained by looking at the <strong>HTTP_X_FORWARDED_FOR</strong> Server Variable.";
    $ua_label="User-Agent sent by the Browser:";
    $ua_info="This is the custom string sent by your browser client including: product name and version, layout engine name and version, & more. For more info, see <a href='https://en.wikipedia.org/wiki/User_agent' target='_blank'>User-Agent on Wikipedia</a>.";
 
    // Very unreliable: can be spoofed/falsified easily.
    $client  = $_SERVER['HTTP_CLIENT_IP'];
 
    // Very unreliable: can be spoofed/falsified easily.
    $forward = $_SERVER['HTTP_X_FORWARDED_FOR'];
 
    // This is the only really reliable information we have, as it is transmitted by the web server that is handling the request. 
    // It can be theoretically falsified, but requires much harder skills than spoofing a header value, as is an entirely different type of hack.
    $remote  = $_SERVER['REMOTE_ADDR'];
 
    echo "<div><span class='title-label'>$remote_label</span><span class='value'>$remote</span><span class='info'>$remote_info</span></div>";
    $css_class="";
    if  (!$client || filter_var($client, FILTER_VALIDATE_IP)) {
        $cls =" class='disabled'";
        $client="N/A";
    }
    else $cls = "";
    echo "<div$cls><span class='title-label'>$client_label</span><span class='value$cls'>$client</span><span class='info'>$client_info</span></div>";
    if  (!$forward || !filter_var($forward, FILTER_VALIDATE_IP)) {
        $cls =" class='disabled'";
        $forward="N/A";
    }
    else $cls = "";
    echo "<div$cls><span class='title-label'>$forward_label</span><span class='value'>$forward</span><span class='info'>$forward_info</span></div>";
 
    $ua = $_SERVER['HTTP_USER_AGENT'];
    if (empty($ua)) {
        $cls =" class='disabled'";
        $ua = "(none - user agent string is empty)";
    }
    else $cls = "";
    echo "<div$cls><span class='title-label'>$ua_label</span><span class='value ua'>$ua</span><span class='info'>$ua_info</span></div>";
}
 
PrintUserExtendedIPInfo();
*/ ?>
Condividi
Autore

Dark

Sviluppatore, analista di progetto, web designer, divulgatore informatico. Lavora come IT Architect per il design e lo sviluppo di siti, servizi, interfacce e applicazioni web e per dispositivi mobili. Microsoft MVP for Development Technologies dal 2018.

Tutti gli articoli di Dark
Discussione

Commenti

Nessun commento.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.


Il periodo di verifica reCAPTCHA è scaduto. Ricaricare la pagina.

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

*/ ?>

Newsletter

I nuovi articoli e gli aggiornamenti sui prodotti, via email.

Ti chiederemo di confermare l'iscrizione via email. Puoi cancellarti quando vuoi; i dati sono trattati come descritto nell'informativa sulla privacy.

{"prefetch":[{"source":"document","where":{"and":[{"href_matches":"/*"},{"not":{"href_matches":["/wp-*.php","/wp-admin/*","/wp-content/uploads/*","/wp-content/*","/wp-content/plugins/*","/wp-content/themes/monstrance-ryadel/*","/wp-content/themes/monstrance/*","/*\\?(.+)","/*ao_noptirocket*","/*jetpack=comms*","/*kinsta-monitor*","/*ao_speedup_cachebuster*","/*removed_item*","/my-account*","/wc-api/*","/edd-api/*","/wp-json*"]}},{"not":{"selector_matches":"a[rel~=\"nofollow\"]"}},{"not":{"selector_matches":".no-prefetch, .no-prefetch a"}}]},"eagerness":"conservative"}]}