PHP script that saves client IP address to a file.

Below I wrote down a simple PHP script that saves client IP address to a file. If the IP address of your home machine periodically changes, you can store it on a web server once a minute by scheduling a task like this:

sudo crontab -u <user> -e
* * * * * wget -q -O /dev/null -o /dev/null "https://yourwebsite.com/ip.php?rig=rig1&password=XXXXX"

where ip.php is the following PHP script:

<?php

//print_r($_GET);

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$clients = array("rig0", "rig1", "rig2", "rig3", "rig4");
$client_password = "XXXXX";

if ($_GET["password"] == $client_password && in_array($_GET["rig"], $clients))
{
    $ip = getRealIpAddr();
    echo $ip;

    $file = "./rigs/" . $_GET["rig"] . ".txt";
    file_put_contents($file, $ip);
}
else
{
    echo "Access denied.";
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *