How to Log IP Addresses of web site visitors?

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

john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

How to Log IP Addresses of web site visitors?

Post by john1in2 »

Hi, I have had a simple web site set up using the GoDaddy hosting service for about 3 years, the only php experience I have is about a year ago I successfully added php code for a text box form that would email me a message, yeah!!!!

I would like to track the IP address of visitors, and have found the following code example on this web page- http://www.willmaster.com/blog/statisti ... resses.php. The code is to be pasted into the html of the web page on which the IP tracking is to be implemented.
(edit-- I'll call this Code #1)

Code: Select all

<?php
$LogFileLocation = "iplog.txt";
$fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'at');
fwrite($fh,date('dMy H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n");
fclose($fh);
?>
 
EDIT- I have also seen this link- http://www.faqts.com/knowledge_base/vie ... 495/fid/51 which gives this code-
(which I'll call Code #2)

Code: Select all

$ip = getenv("REMOTE_ADDR");
$log = "\n$ip";
$fa = fopen("login", "a");
$login = "$log";
fwrite($fa, $login);
fclose($fa);
 
Neither works. No log file (or any file) is created. There is also a note about 777 permissions which I don't get.
Also, when I did the php code years ago, I noticed that I could not use the fopen command, it would never work (is this a GoDaddy issue?).

Anyway, what is the best way to create a log file of the IP addresses of all visitors to my web site?
Last edited by john1in2 on Tue Jan 13, 2009 6:41 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:

Re: How to Log IP Addresses of web site visitors?

Post by John Cartwright »

Code: Select all

$fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'at');
There is no much filemode "at". I think you wanted "a".
john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

Re: How to Log IP Addresses of web site visitors?

Post by john1in2 »

Jcart wrote:

Code: Select all

$fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'at');
There is no much filemode "at". I think you wanted "a".
Wow, quick response! I noticed that 'at' was not consistent with the switches I remembered but neither 'a' nor 'at' work. No file is created.

It may help to see if there some code I can put into a web page just to test that I am able to make some file, any file?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Log IP Addresses of web site visitors?

Post by requinix »

Jcart wrote:There is no much filemode "at". I think you wanted "a".
Actually...
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
The thing about 0777 (the zero is important, but without it we know what you mean) is to make sure your script has the permission to create a new file. You have two options:
- Change the permissions on the directory where this log file goes. Worst choice.
- Create the iplog.txt file yourself and change the permissions on it so that your script can write to it. Definitely better.
Over FTP you can upload a blank file then change the permissions to 0666 (which is better than 0777). Most FTP clients can do it, exactly how depends on the program.
If you don't have a place to enter a number then the file should be readable and writable by "owner", "group", and "other".
john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

Re: How to Log IP Addresses of web site visitors?

Post by john1in2 »

Thanks for the info and for another quick response.

I copied a blank file 'iplog.txt' to the server and using Explorer clicked 'properties' and under 'general' checked the boxes so that
Owner can read & write but not execute
Group can read & write but not execute
All Users can read & write but not execute

Still, the file is zero bytes after loading the test web page with the first version of the html code (using 'a' not 'at'). Nothing makes it in there.

EDIT- I changed permissions so that all 3 groups are permitted to 'execute' as well and it didn't make a difference.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Log IP Addresses of web site visitors?

Post by requinix »

DOCUMENT_ROOT doesn't necessarily have a trailing /, and I'm pretty sure having two is okay everywhere that matters, so

Code: Select all

$LogFileLocation = "/iplog.txt";
john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

Re: How to Log IP Addresses of web site visitors?

Post by john1in2 »

OK, tried it but the leading "/" did not work, still not writing to the file.

Most recent version of code #1

Code: Select all

<?php
$LogFileLocation = "/iplog.txt";
$fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'a');
fwrite($fh,date('dMy H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n");
fclose($fh);
?>
Tried changing the single quotes to double but that didn't work either.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Log IP Addresses of web site visitors?

Post by requinix »

Code: Select all

<?php
error_reporting(E_ALL|2048);
ini_set("display_errors", 1);
 
$LogFileLocation = "/iplog.txt";
$fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'a');
fwrite($fh,date('dMy H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n");
fclose($fh);
?>
Now try.
john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

Re: How to Log IP Addresses of web site visitors?

Post by john1in2 »

OK, I have copied your code in.

Perhaps you don't realize just how clueless I am, meaning I may have made a much more basic mistake somewhere else.

Here is the entire html file.

Code: Select all

<html>
<body bgcolor="#d0d0d0">
<head>
<center>
<h1 style="color:blue">Test Page 1</h1>
<p>Should write the accessing IP Address to iplog.txt in this (root) directory</p>
</center>
 
<?php
error_reporting(E_ALL|2048);
ini_set("display_errors", 1);
 
$LogFileLocation = "/iplog.txt";
$fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'a');
fwrite($fh,date('dMy H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n");
fclose($fh);
?>
 
</body>
</html>
the file iplog.txt is in the same directory and everyone has permissions set up to read/write/execute.

When I access the html file, nothing happens. There is no error either. It just displays like it should and the file iplog.txt is still empty.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Log IP Addresses of web site visitors?

Post by requinix »

Ah.

To use PHP it must be installed and configured. PHP files also need a .php extension, though this can be changed if you don't like it.
Are you missing out on either of those?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to Log IP Addresses of web site visitors?

Post by John Cartwright »

tasairis wrote:
Jcart wrote:There is no much filemode "at". I think you wanted "a".
Actually...
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
The thing about 0777 (the zero is important, but without it we know what you mean) is to make sure your script has the permission to create a new file. You have two options:
- Change the permissions on the directory where this log file goes. Worst choice.
- Create the iplog.txt file yourself and change the permissions on it so that your script can write to it. Definitely better.
Over FTP you can upload a blank file then change the permissions to 0666 (which is better than 0777). Most FTP clients can do it, exactly how depends on the program.
If you don't have a place to enter a number then the file should be readable and writable by "owner", "group", and "other".
Ah, good to know. Thanks.
john1in2
Forum Newbie
Posts: 11
Joined: Tue Jan 13, 2009 5:50 pm

Re: How to Log IP Addresses of web site visitors?

Post by john1in2 »

tasairis wrote:Ah.

To use PHP it must be installed and configured. PHP files also need a .php extension, though this can be changed if you don't like it.
Are you missing out on either of those?
Ahhh.....Yes I figured I was doing something really stupid that I should have explained!
Hmmm....well I am not sure if everything is configured.

To do my text box php, I use an HTML form that calls a php file.

html code-

Code: Select all

<form method="post" action="sendmail.php">
  Email Address (optional): <input name="email" type="text" /><br />
  Comment:<br />
  <textarea name="message" rows="5" cols="40">
  </textarea><br />
  <input type="submit" />
</form>
contents of "sendmail.php"-

Code: Select all

<?
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;
  mail( "me@mywebsite.com", "Feedback From My Web Site",
    $message, "From: $email" );
header( "Location: ThankYouForYourComment.html" );
?>
This works so I was hoping to do the same thing with the IP Address log. Do you think it can be done?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to Log IP Addresses of web site visitors?

Post by requinix »

If sendmail.php works (you get an email) then PHP works.
But how do you intend to do "the same thing"? It's kinda different things, you know?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How to Log IP Addresses of web site visitors?

Post by s.dot »

I think what tsairis is saying is that the HTML file you're loading in your browser should end with a .php extention and not .html if that is what you have it set as.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How to Log IP Addresses of web site visitors?

Post by s.dot »

Try this:

Code: Select all

<?php
 
//error handling
ini_set('display_errors', true);
error_reporting(E_ALL);
 
//location of log file
$LogFileLocation = $_SERVER['DOCUMENT_ROOT'] . '/iplog.txt';
 
//does log file exist?
if (!file_exists($LogFileLocation))
{
    die('Cannot locate log file (' . $LogFileLocation . ')');
}
 
//can we open it?
if (!$fh = fopen($LogFileLocation, 'at'))
{
    die('Cannot open log file for reading');
}
 
if (!fwrite($fh, date('dMy H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n"))
{
    die('Cannot write to log file.');
}
 
fclose($fh);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply