Skip to content

Instantly share code, notes, and snippets.

@vldmitrofanov
Created June 10, 2023 21:03
Show Gist options
  • Save vldmitrofanov/e8ab5482f94ca91596c471f6f2433505 to your computer and use it in GitHub Desktop.
Save vldmitrofanov/e8ab5482f94ca91596c471f6f2433505 to your computer and use it in GitHub Desktop.
Logging up to a server your local IP obtained via DHCP
<?php
// On the server to get list of logged IPs
$content = file_get_contents("/tmp/ip.html");
echo $content;
<?php
// ON THE SERVER
$html_file = "/tmp/ip.html";
$data = json_decode( file_get_contents('php://input') );
$error = false;
header('Content-Type: application/json; charset=utf-8');
if(!empty($data)){
if(isset($data->host) && isset($data->ip)){
$date = date("F jS, Y", strtotime("now"));
$ip = $_SERVER["REMOTE_ADDR"];
$str = "<strong>$data->host</strong>: <span>$data->ip [$ip]</span> <em>$date</em><br />".PHP_EOL;
$flags = 0;
if(file_exists($html_file)){
$flags = FILE_APPEND;
}
file_put_contents($html_file, $str, $flags);
echo json_encode(['success' => true]);
echo PHP_EOL;
} else {
$error = true;
}
} else {
$error = true;
}
if($error){
echo json_encode(['success' => false]);
echo PHP_EOL;
}
# ON THE CLIENT
# don't forget to do
# sudo chmod +x /etc/rc.local
...
hostname=$(hostname -f)
ip=$(hostname -I)
curl -X POST https://<your server>/network-tools/post_ip.php -H 'Content-Type: application/json' -d "{\"host\":\"$hostname\",\"ip\":\"$ip\"}"
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment