Link to home
Start Free TrialLog in
Avatar of neonlights
neonlights

asked on

keep track of visitors ip address and the time they spent on my site

Hello,

I am trying to create a php code - that open a .txt file in my root directory and write whoever visits my site - ip address, may be location (from where.. ) and time of enter..

I do not want to use database.. I am wondering with the txt file.. and also wondering what will happen if two users trying enter my site at the same time.. about the .txt file..

Here is what I have - on top of my index.php page:

<?
session_start();
$_SESSION["currip"] = $_SERVER['REMOTE_ADDR'];
$_SESSION["timeofentry"] = date("Y-m-d h:i:s",time());
$file = 'dump.txt';
$fh = fopen($file, 'w') or die('Could not open file');
fwrite($fh, '$_SESSION["currip"]') or die('Could not write to file');
fclose($fh);
/?>

Also that my die() message - ~~ I do not want to disturb the users with my error messages. I thought that my host will have those info, but they do not.

any suggestions? Again, I am learning PHP and want to practice...

Thanks all
Avatar of stormist
stormist

$ipaddress = $REMOTE_ADDR;
$domain = GetHostByName($REMOTE_ADDR);
$ref = $_SERVER['HTTP_REFERER'];
$time = date('Y-m-d');
$stringData= 'IP address: '.$ipaddress.'Domain: '.$domain.'Came from: '.$ref.'Time: '.$time;
$myFile = "dump.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);


The chances that the file would attempt to be opened at the exact same time is not likely unless you have a very busy site. In that case you need to use a database. In any case, what will happen is PHP will attempt to open it and give an error for the second user because its already opened. No biggy. You can remove the error message warning if you want. (remove or die("can't open file"); )This should give you all the information you need, Let me know if you have questions :)
Avatar of neonlights

ASKER

Hi Stormist, Thank you very much for your answer.

your code should read: ?? -

$ipaddress = $_SERVER['REMOTE_ADDR'];  ********
$domain = GetHostByName($ipaddress);  ********
$ref = $_SERVER['HTTP_REFERER'];
$time = date('Y-m-d');
$stringData= 'IP address: '.$ipaddress.'Domain: '.$domain.'Came from: '.$ref.'Time: '.$time;
$myFile = "dump.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);

I changed them.. but, I am still getting errors here:

$ref = $_SERVER['HTTP_REFERER'];

Notice: Undefined index: HTTP_REFERER

and Warning: fopen(dump.txt): failed to open stream: Permission denied in
I am just going to check the file permission - what it should be? do you know?
HI Stormist,

I tried many different ways - from this site - previously asked question..

server is running on windows and IIS - my web server..

I was not able to use:

Notice: Undefined index: DOCUMENT_ROOT

and Undefined index:  'HTTP_REFERER'

Do you know why?

Thanks
SOLUTION
Avatar of WindowsMax
WindowsMax

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot to mention that I tested the code and got the following when I directly requested the page after echoing $ref;

undefined or direct request

However, I created a test file and put a link to the test php file and got the following:

http://192.168.1.81:8009/test.html
Hi WindowMax,

I noticed that from yours:

$host=apache_request_headers();

My web server is IIS on windows.. does it matter?

and ingwa,

I tested your first line:

<?php
if(isset($_SERVER['HTTP_REFERER']))
{
      $ref = $_SERVER['HTTP_REFERER'];
}
else
{
      $ref= "undefined or direct request";
}
echo $ref;

?>

and getting "undefined or direct request";

I also have a secure directory in my web server https:// as well as mysql database.

The reason that I did not want to use the mysql because, I will be accessing the database from my http://www.mysite.com/index.php - which is not secure..  I have to give my user name and the password for my server to open the database. That is the reason that I am going with txt file.

I am new to this. So, if I am wrong, please suggest me the best way to do this.

Thanks all.

Hello again neonlights. Your username and password are totally safe inside a PHP file. PHP is a server side language, which means the only thing that gets rendered to a browser is what you tell it to render. They cannot just open up a php file and look inside of it. You could write all your credit cards inside the php code, and if you didn't echo them it's safe. So bottom line is a database is faster, more secure, and better in so many ways. So if security is your concern, by all means use a database. :) BTW,  Is there any particular reason you are not using APACHE?
Thanks again stormist. OK. I will do the code for it and show it to you. to see if everything is ok.

and about the APACHE - my host - on windows, IIS..  that is what he said..

thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am going to use all these:

$ipaddress = $_SERVER['REMOTE_ADDR'];  ********
$domain = GetHostByName($ipaddress);  ********
$ref = $_SERVER['HTTP_REFERER'];
$time = date('Y-m-d');

would you tell me why the HTTP_REFERER does not work?

do you know?
OK...

One question about the $ipaddress  -- I thought you have to use: $_SERVER['REMOTE_ADDR'];  

Is it so?
Neonlights, on your testing server, create a file called test.htm and link it to the php file that you are using to call the referring url.  You should see that with my code above it should actually echo out the address of the previous page.

Reason for this is, when you go to a website by typing in an address, you haven't actually clicked a link, and therefore how can you have a referring url.  There isn't one.  This is the same with a link on your desktop or favourites, you aren't clicking a link from an existing site or an existing url.

Therefore, if you try the above suggestion of setting up a dummy file with a link to the page, you should actually see the link.

Hope this helps.
Reason it doesn't work is as stated above or the user has his browser set to block it. It's all optionable, and the user can even insert a referring URL that he didnt' actually come from. :)
"you should actually see the link" should read you should acutally see the address of the test.htm page, along with server address, mode of web access (http:// or https://) and even a port number if there is one (e.g. http://www.myserver.com:8080/test.htm - 8080 is the port number) and the full directory structure and file name.
Ah yes, thats better code on the $_SERVER['REMOTE_ADDR'];  part.
hi ingwa,

I almost got it with stormist suggestion using database. I thank you for your url.

Hi stormist: this is what I have: in my php code

$connection = mysql_connect($hostname, $username, $password) or die('Unable to connect');
// select database
mysql_select_db($username) or die('Unable to select database');

$ipaddr = $_SERVER['REMOTE_ADDR'];  ;
$vdomain = GetHostByName($ipaddr);
$timeofentry = date("Y-m-d h:i:s",time());
$query = "INSERT INTO visitorstomysite (ipaddr , vdomain, timeenter) VALUES ('".$ipaddr."', '".$vdomain."', '".$timeofentry."')";
$result = mysql_query($query) or die("Error in query. " . mysql_error());
mysql_close($connection);

few points:

should I turn off the die() - no message?

and I checked the db and there is 2 ip addressess - one for ipaddr and vdomain

how do I use this information?  do not know much about it..

thank you very much.

One more thing - in my database:

ipaddr and vdomain are same ipaddress - what is that mean?
ah you found another mistype :)
change
$vdomain = GetHostByName($ipaddr);
to
$vdomain = gethostbyaddr($ipaddr);

more info on this here if you're curious:
http://us3.php.net/manual/en/function.gethostbyaddr.php

Perfect...

Thank you very much.
You're welcome. Good luck!
Glad I was of some assistance neonlights and thanks for the grade.  Have a great day.