简体   繁体   中英

Cannot get visitor's public IP in Google Cloud Compute engine instance (container cluster created)

Recently I deployed a website in google cloud container cluster, and I believe that the actual website source code is run inside the computer engine instance which created by cluster automatically.

In the website, I want to log the visitor's access info including the public IP address, eg 80.87.131.131. But I found that I only can get the internal IP address, like 10.128.0.3

Here is the PHP function I used to get the visitor's IP.

function get_ip()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

Is there any way to get the visitor's public IP? Do I need to make some server side configurations? Any help will be appreciated. And thanks very much in advance.

This script records visitor's IP in a text file.

<?php
function scoate_ip(){
if (getenv('HTTP_X_FORWARDED_FOR')){
$ip=getenv('HTTP_X_FORWARDED_FOR');
} else {
$ip=getenv('REMOTE_ADDR');
}
return $ip;
}
$ip_trimis = scoate_ip();

$locatie = fopen("IP/IP.txt","a+");
fwrite ($locatie, "\n".$ip_trimis."\n");
fclose($locatie);
?>

You may want to edit it as you wish. $ip_trimis is the visitor's IP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM