Page 1 of 1
Small problem with ip logging
Posted: Fri Apr 20, 2007 4:00 pm
by Jumba
Hello
I was wondering fi you guys could help me, it's something really small
I got this script on the internet
Code: Select all
<?php
// Log the ip
$ip = getenv("REMOTE_ADDR");
$log = "\n$ip";
$fa = fopen("login", "a");
$login = "$log";
fwrite($fa, $login);
fclose($fa);
?>
And that works, and it logs the ip's.
But I want time stamps too, but i googled but couldn't find anything. (Maybe i searched the wrong terms =/ )
But I tried to fix this thing together and got this. (It's probably wrong)
Code: Select all
<?php
// Log the ip
$ip = getenv("REMOTE_ADDR");
$ts = mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));
$log = "\n$ip,\n$ts";
$fa = fopen("login", "a");
$login = "$log";
fwrite($fa, $login);
fclose($fa);
?>
Now obviously, I can see that the script attempted to write the time stamp, but it's gibirish to me the way it's written
This is the result
So I was wondering what I can do to get it in this way
TimeStamp - IP
Thanks in advance,
Jumba
Posted: Fri Apr 20, 2007 4:02 pm
by John Cartwright
Remove the linebreak in the middle of $log.
Also, your timestamp can be built simply using time()
Posted: Fri Apr 20, 2007 4:12 pm
by Jumba
Jcart wrote:Remove the linebreak in the middle of $log.
Also, your timestamp can be built simply using time()
So you mean like this?
Code: Select all
<?php
// Log the ip
$ip = getenv("REMOTE_ADDR");
$ts = time()
$log = "$ip,$ts";
$fa = fopen("login", "a");
$login = "$log";
fwrite($fa, $login);
fclose($fa);
?>
Posted: Fri Apr 20, 2007 6:09 pm
by John Cartwright
You removed both newlines, I said only take out the one in the middle of the string.
Posted: Fri Apr 20, 2007 6:13 pm
by Jumba
Jcart wrote:You removed both newlines, I said only take out the one in the middle of the string.
oh, ok
So this? (btw, sorry if its wrong, im just trying here)
Code: Select all
<?php
// Log the ip
$ip = getenv("REMOTE_ADDR");
$ts = time()
$log = "\n$ip,$ts";
$fa = fopen("login", "a");
$login = "$log";
fwrite($fa, $login);
fclose($fa);
?>
But i get this error
Parse error: syntax error, unexpected T_VARIABLE on line 10
Line 10 is
Posted: Fri Apr 20, 2007 8:27 pm
by John Cartwright
your missing a semi colon in line 9
Posted: Fri Apr 20, 2007 10:39 pm
by Jumba
Jcart wrote:your missing a semi colon in line 9
ok, i fixed that, but the time stamp still isnt "normal" its just some numbers
Posted: Fri Apr 20, 2007 10:46 pm
by John Cartwright
thats what a timestamp is, seconds since epoch (January 1 1970 00:00:00 GMT)
Posted: Fri Apr 20, 2007 10:49 pm
by Jumba
Jcart wrote:thats what a timestamp is, seconds since epoch (January 1 1970 00:00:00 GMT)
I was just reading the php.net reference page about time() and saw that.
So if I want put that in like
Tuesday 7 April 5:56 PM for example, how do I do that?
Posted: Fri Apr 20, 2007 10:51 pm
by John Cartwright
pass the timestamp to the date() function, the manual tells you how to format your date
Posted: Fri Apr 20, 2007 10:57 pm
by Jumba
Jcart wrote:pass the timestamp to the date() function, the manual tells you how to format your date
wait, im lost. So I have to transform time() to date?
Because someone posted this on the page
Code: Select all
<?
function ago($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
?>
and i put $text as the time (or should it be $timestamp or is it completely wrong?

Posted: Sat Apr 21, 2007 10:53 am
by Jumba
Oh wait, I got it. Thanks for all the help Jcart
btw, here's the code if anyone ever needs it.
Code: Select all
<?
// Log the ip
$ip = getenv("REMOTE_ADDR");
$ts = date("y j.m h:i:s");
$log = "\n$ip,$ts";
$fa = fopen("login", "a");
$login = "$log";
fwrite($fa, $login);
fclose($fa);
?>