Hey! OK, I've got a problem that I hope someone can help me with!
I have a web site at: http://home.hia.no/~sdhild99/
I am a PHP newbie, and I wanted to create a log file of all the visitors that visited my site. K, I use a php counter that sets cookies, in order to count every visitor one time only (as long as they receive the cookie)
I've written a script that logs the following info about every hit/visitor on my site:
- Time and date
- Hostname
- IP address
- Referer
- Browser and OS
- Remote Port
- URI
OK, the script (counter included) looks like this:
<?
$cookie = ja; // Hvis counteren kun skal telle én gang pr. computer, skal der stå "ja", ellers "nej"
$tid = 2592000; // Tid der skal gå, før counteren tæller igen (hvis man bruger cookie) I dette tilfellet: 1 mnd
$file = "count.txt"; // Fil, som den skal gemme countertallet i
if($count == "yeps") {
} else {
if(file_exists($file)) {
$fil = fopen($file, "r");
$count = fread($fil, 8);
fclose($fil);
} else
$count=0;
$count++;
$fil = fopen($file, "w");
fwrite($fil, $count);
fclose($fil);
if($cookie == "ja") {
setcookie("count","yeps",time()+$tid);
}
}
?>
<?php //Script modified and improved by Sven David Hildebrandt - webmaster@eternityweb.net
//Script version 2.0
$dummydocument = fopen("/adh/svale/u1/sdhild99/www_docs/testing/visitors2.html", "a");
if ($HTTP_COOKIE_VARS["count"]) {
fread($dummydocument, 1);
}
else {
$visitor_time = date("F jS Y, h:iA");
$visitor_info = gethostbyaddr($REMOTE_ADDR);
if ($REMOTE_ADDR == "") $visitor_info2 = "There is No address";
else $visitor_info2 = $REMOTE_ADDR;
if ($HTTP_REFERER == "") $visitor_info3 = "No referer";
else $visitor_info3 = $HTTP_REFERER;
if ($HTTP_USER_AGENT == "") $visitor_info4 = "No browser or other browser used";
else $visitor_info4 = $HTTP_USER_AGENT;
if ($HTTP_ACCEPT_LANGUAGE == "") $visitor_info5 = "Could not detect language";
else $visitor_info5 = getenv("HTTP_ACCEPT_LANGUAGE");
if ($REMOTE_PORT == "") $visitor_info6 = "Could not detect remote port";
else $visitor_info6 = getenv("REMOTE_PORT");
/
if ($HTTP_HOST == "") $visitor_info7 = "Could not detect host";
else $visitor_info7 = $HTTP_HOST;
/
if ($REQUEST_URI == "") $visitor_info7 = "Did not type URI";
else $visitor_info7 = $REQUEST_URI;
$fp = fopen("/adh/svale/u1/sdhild99/www_docs/testing/visitors.php3", "a");
fputs($fp, "<br><b>New Visitor:</b> $visitor_time
<br>
<table width=\"100%\">
<tr bgcolor=\"#B0C4DE\"><b>Hostname: </b> $visitor_info</tr>
<tr bgcolor=\"#D3D3D3\"><b>IP: </b> $visitor_info2</tr>
<tr bgcolor=\"#B0C4DE\"><b>Referer: </b> $visitor_info3</tr>
<tr bgcolor=\"#D3D3D3\"><b>Browser & OS: </b> $visitor_info4</tr>
<tr bgcolor=\"#B0C4DE\"><b>Browser Language: </b> $visitor_info5</tr>
<tr bgcolor=\"#D3D3D3\"><b>Remote Port: </b> $visitor_info6</tr>
<tr bgcolor=\"#B0C4DE\"><b>Typed URI: </b> $visitor_info7</tr>
</tr></table>
");
fclose($fp);
}
?>
The script writes the info above into a text file called "visitors.php3"
OK, now the crunch: To this log file I'd also like to add the following info:
- User's screen resolution
- User's color depth
- Whether java is enabled in the browser or not
OK, I know that PHP only can obtain info about from headers or something.
I know that what I want, cannot be directly obtained by PHP.
So I included javascript, and made the following change to my script, in order to obtain the info I wanted (counter script not included):
<?php //Script modified and improved by Sven David Hildebrandt - webmaster@eternityweb.net
//Script version 2.0
$dummydocument = fopen("/adh/svale/u1/sdhild99/www_docs/testing/visitors2.html", "a");
if ($HTTP_COOKIE_VARS["count"]) {
fread($dummydocument, 1);
}
else {
$visitor_time = date("F jS Y, h:iA");
$visitor_info = gethostbyaddr($REMOTE_ADDR);
if ($REMOTE_ADDR == "") $visitor_info2 = "There is No address";
else $visitor_info2 = $REMOTE_ADDR;
if ($HTTP_REFERER == "") $visitor_info3 = "No referer";
else $visitor_info3 = $HTTP_REFERER;
if ($HTTP_USER_AGENT == "") $visitor_info4 = "No browser or other browser used";
else $visitor_info4 = $HTTP_USER_AGENT;
if ($HTTP_ACCEPT_LANGUAGE == "") $visitor_info5 = "Could not detect language";
else $visitor_info5 = getenv("HTTP_ACCEPT_LANGUAGE");
if ($REMOTE_PORT == "") $visitor_info6 = "Could not detect remote port";
else $visitor_info6 = getenv("REMOTE_PORT");
$visitor_info7 = "<script language=\"javascript\">document.write(screen.width + \" x \" + screen.height)</script>";
$visitor_info8 = "<script language=\"javascript\">document.write(screen.colorDepth + \" bit\")</script>";
$visitor_info9 = "<script language=\"javascript\">if(navigator.javaEnabled()){document.write(\"Yes\")} else{document.write(\"No\")}</script>";
$fp = fopen("/adh/svale/u1/sdhild99/www_docs/testing/visitors.php3", "a");
fputs($fp, "<br><b>New Visitor:</b> $visitor_time
<br>
<table width=\"100%\">
<tr bgcolor=\"#B0C4DE\"><b>Hostname: </b> $visitor_info</tr>
<tr bgcolor=\"#D3D3D3\"><b>IP: </b> $visitor_info2</tr>
<tr bgcolor=\"#B0C4DE\"><b>Referer: </b> $visitor_info3</tr>
<tr bgcolor=\"#D3D3D3\"><b>Browser & OS: </b> $visitor_info4</tr>
<tr bgcolor=\"#B0C4DE\"><b>Browser Language: </b> $visitor_info5</tr>
<tr bgcolor=\"#D3D3D3\"><b>Remote Port: </b> $visitor_info6</tr>
<tr bgcolor=\"#B0C4DE\"><b>Screen Resolution: </b> $visitor_info7</tr>
<tr bgcolor=\"#D3D3D3\"><b>Colour Depth: </b> $visitor_info8</tr>
<tr bgcolor=\"#B0C4DE\"><b>Java Enabled?: </b> $visitor_info9</tr>
</tr></table>
");
fclose($fp);
}
?>
Now I get the desired info, but the PROBLEM is that when I VIEW "visitors.php3", the 3 variables detected with the javascript (screen resolution, color depth, and java)
show MY stats! It shows MY screen resolution and so on, for EVERY record in the log file.
In other words, the previous visitor's data is not properly recorded into the file, or it's being overwritten when I look at it.
Can anybody help?
Sven David