Small problem with ip logging

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Small problem with ip logging

Post 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

Code: Select all

1177101654, 
69.79.*.*
So I was wondering what I can do to get it in this way
TimeStamp - IP
Thanks in advance,

Jumba
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Remove the linebreak in the middle of $log.
Also, your timestamp can be built simply using time()
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Post 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);
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You removed both newlines, I said only take out the one in the middle of the string.
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Post 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

Code: Select all

$log = "/n$ip,$ts";
Last edited by Jumba on Fri Apr 20, 2007 10:40 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

your missing a semi colon in line 9
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

thats what a timestamp is, seconds since epoch (January 1 1970 00:00:00 GMT)
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

pass the timestamp to the date() function, the manual tells you how to format your date
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Post 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? :?
Jumba
Forum Newbie
Posts: 23
Joined: Mon Apr 09, 2007 7:24 pm

Post 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);
?>
Post Reply