Reputation: 1
I have made simple PHP IP logger and need some help. Right now, I have set it to grab IP and make new line, ready to log the next (I have to use HTTP_CF_CONNECTING_IP instead of REMOTE_ADDR because I'm on cloudflare). What I want to know is: how do I make it show this format instead of just IP: IP date time. I want all three of those not just IP each separated by space. ~~Thank you
<?php
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
$file = "ip.txt";
$txtfile = fopen($file, "a");
fwrite($txtfile, $ip."\n");
fclose($txtfile);
?>
Upvotes: 0
Views: 1759
Reputation: 1
You can use this code:
<?php {
$date = fopen("thefile.txt","a+"); //open log file
fwrite($date, date("Y-m-d H:i:s ")); //writes date and time
fclose($date); //closes the file
$ip = $_SERVER['REMOTE_ADDR']; //get supposed IP
$handle = fopen("thefile.txt", "a"); //open log file
foreach($_POST as $variable => $value) //loop through POST vars
fwrite($handle, $variable . "+" . $value . "\r\n");
fwrite($handle, "$ip\r\n"); //writes down IP address and adds new line
fclose($handle); //close the file
}
?>
You will see this on the the file.txt:
yyyy-mm-dd hh:mm:ss xxx.xxx.xxx.xxx
yyyy-mm-dd hh:mm:ss xxx.xxx.xxx.xxx
yyyy-mm-dd hh:mm:ss xxx.xxx.xxx.xxx
I mean with numbers not alphabets like this 2021-01-01 24:59:59 *someones ip address*
, every time the website gets load it will write down the date, time and IP address and new line as seen up here.
NOTE: This will not show on the loaded websties view-source:https://expamle.com
cause this script will execute server site, if your hosted website supports it. You can also mess around with it
Upvotes: 0
Reputation: 54796
Concatenate ip with result get by date
function:
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
$file = "ip.txt";
$txtfile = fopen($file, "a");
// here:
$line = $ip . " " . date("d.m.Y H:i:s");
// for your required format:
$line = $ip . " | " . date("d.m.Y | H:i:s");
// write $line instead of $ip to a file
fwrite($txtfile, $line . "\n");
fclose($txtfile);
Upvotes: 1