Reputation: 685
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, e.g. 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.
Upvotes: 2
Views: 1014
Reputation: 491
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.
Upvotes: 1