Page 2 of 2

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

Posted: Tue Jan 13, 2009 9:58 pm
by john1in2
scottayy wrote:Try this:
OK, I've tried that code, both in test1.html and in test1.php.

Neither file behaves differently- they just show the web page and don't add anything to the iplog.txt file. No errors on screen or in the file.

Also, I tried the earlier version renamed .php instead of .html. Nothing happens there either.
But how do you intend to do "the same thing"? It's kinda different things, you know?
What I mean, should I try to call to a php file from the html file to accomplish the task?

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

Posted: Tue Jan 13, 2009 10:35 pm
by s.dot
HTML can be embedded within PHP code. If you have an HTML page with php code contained within it, always (well, usually) use .php as the extension.

The .php extension is recognized by the web server and the code is sent off to the PHP program to be parsed and HTML returned.

So make sure that all of the pages where you are using PHP code within them must have the .php file extension.

How do you know they are not updating the log file? After loading the page.. do you download/view the NEW log file from your server?

I am thinking that you don't have sufficient permissions to update the file contents, if this is not the case. Try running this small piece of code within a .php extension file

Code: Select all

<?php
 
echo '<pre>';
var_dump($a = is_writable($_SERVER['DOCUMENT_ROOT'] . '/iplog.txt'));
echo '</pre>';
If the above prints true, then that means that you can indeed write to the file.
If it prints false, then you do not have sufficient permissions on the file to write to it.

Also, if you are copying code from this forum, # marks will appear in front of each line of code, such as

# line 1
# line 2
# line 3

Make sure you remove those # signs from the beginning of the code you copied.

And finally, when you're getting a blank white web page.. make sure it's truly nothing there. Right click inside the web page and go to 'view source' from the menu. See what's there. :)

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

Posted: Tue Jan 13, 2009 10:54 pm
by john1in2
I appreciate the comments and the code segment. I guess I deserve it since I didn't even remember that you can save the file as a .php. ouch!

I load this latest code sample into 'test.php' and when it is accessed it shows the same old screen and we have some results---- it says
bool(true)
Does this mean I should be able to write the file?

Yeah, I get it about the line numbers. And yes, I actually copy the iplog.txt file to my HD and look at it with a text editor to verify that it is empty. And its size is 0 bytes= empty. Yet when I ask Explorer to show me the source on the server iplog.txt file I get this-

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY><PRE></PRE></BODY></HTML>
I assume this is just the browser adding stuff to an empty file, or nonvisible control characters/codes, since when I copy the file over from server to HD and look at it with a text editor there is nothing there and its 0 bytes.

Are we making progress? Happy to run any other tests to see why the log is not writing, or to verify that it isn't.

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

Posted: Tue Jan 13, 2009 11:52 pm
by s.dot
The bool(true) means that it is able to write to your log file. And, with the code posted in the forums the log file *is* being written to. If I had to place my bets, I'd say you're somehow not viewing an updated copy of the log file. :P

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

Posted: Wed Jan 14, 2009 12:09 am
by john1in2
Nope, just wiped all the files and tried again using a .php filename. I can confirm that when I access the .php file there are no errors. If I click on "view source" in Explorer (of the php file) the php code is gone- this confirms that the php code was extracted and is running on the server, right?

But its not writing to the logfile.

I think we demonstrated that I should be able to write to the log file not that I actually can. How about a segment to write "testing123" to the file or something similar just to verify that the file can be successfully written to? Also what about the "view source" content of the empty logfile that I posted- is this expected or unexpected/ consistent or inconsistent with an empty text file?

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

Posted: Wed Jan 14, 2009 6:32 pm
by john1in2
Hey, where is the help???
Ha! I got it to work.

The fix was changing lines 2-3, the part which specifies the file and to open it; so lines 2-3 of this code

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);
?>
become line 2 of this code-

Code: Select all

<?php 
$file = fopen("iplog.txt","a") or exit ("Unable to open file");
fwrite($file, date('dMy H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n");
fclose ($file);
?>
Anyone know why one works but not the other?
Anyway, thank you everyone for your help. You got me far enough along to get me situated so I could get it working ... never would have been able to do it.