Page 1 of 1
Counter/Log?
Posted: Wed May 28, 2003 7:23 pm
by j0ker21m
For my website, I would like to make a hits counter/log. What I would like is to put a script on my page that records some visitor information, then writes them to a txt file. I am confused as to how to get the information (IP, time, ect.) and then transfer it to a txt file. Do I create the script all on one page, then just include it on all my webpages? Can I retrieve the time/ip/ext functions on the same page as my fopen/fwrite codes? Am I confusing you? I'm confusing myself...grrr...any suggestions?
Posted: Wed May 28, 2003 8:55 pm
by AVATAr
For the user IP see $_SERVER[] variable:
http://www.php.net/manual/en/reserved.v ... les.server ($_SERVER['REMOTE_ADDR'])
For time/date :
http://www.php.net/manual/en/function.date.php
The just write them into a file:
http://www.php.net/fwrite
good luck

Posted: Wed May 28, 2003 11:02 pm
by j0ker21m
OK..here is the code I came up with:
Code: Select all
<?
// Set Variables
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("F j, Y, g:i a");
$url = "http://jokersdomain.com";
$page = $_SERVER['REQUEST_URI'];
// End Variables
//Open file then Write to file
$log="log.txt";
if(!$listfile=fopen ($log, 'a')) {
print "cannot open $log for writing.\n";
exit;
} else {
fwrite($listfile,"<TR> \n <td>$date</td> \n <td>$ip</td> \n <td><a href=$url$page>$url$page</a></td> \n </TR> \n");
if(!fclose($listfile)){
print "cannot close $log";
exit;
}
}
?>
If I access this page directly, it works fine. If I use it as an
include("") in another page, it only shows the URL of the log.php page. Any suggestions on how to change this to show visitors IP, and the page they visited?
Here is the results page:
http://jokersdomain.com/links/
Posted: Wed May 28, 2003 11:23 pm
by AVATAr
what's the problem.. i login your page and the result is this:
May 28, 2003, 10:21 pm 200.40.165.160
http://jokersdomain.com/links/log.php
its ok.. its my IP
Posted: Wed May 28, 2003 11:26 pm
by j0ker21m
Go to
http://jokersdomain.com and then go to
jokersdomain.com/links/ to see the log of the first url. It should log
http://jokersdomain.com in the log...but instead logs the
http://jokersdomain.com/links/log.php instead. the log.php is my "include" file on the main url. I want it to log the page that was visited instead of the log.php everytime. (Is that clear enough? Dunno if I clarified very well)
Posted: Wed May 28, 2003 11:40 pm
by AVATAr
try:
Code: Select all
$page = $_SERVERї'HTTP_REFERER'];
insted of :
Code: Select all
$page = $_SERVERї'REQUEST_URI'];
Posted: Wed May 28, 2003 11:41 pm
by j0ker21m
That only returns "
http://jokersdomain.com" not the page that loaded it.
Posted: Wed May 28, 2003 11:54 pm
by AVATAr
i think i just understand you.
try using $_SERVER['PHP_SELF'] -> it will give you the filename of the currently executing script, relative to the document root.
and $_SERVER['SERVER_NAME'] if you want: The name of the server host under which the current script is executing
Posted: Thu May 29, 2003 11:20 am
by j0ker21m
That didnt seem to work neither. Maybe I'll try a different way. Is there a way to transfer variables from one page to the other without using a form?
Posted: Thu May 29, 2003 11:23 am
by AVATAr
read sticky:
viewtopic.php?t=511
you post your form and you receive the variables in the $_POST['variable']
Posted: Thu May 29, 2003 5:05 pm
by j0ker21m
Thanx.
I was hoping to x-fer variables between pages "without" using a form. I appreciate your help though.
Posted: Fri May 30, 2003 2:46 am
by twigletmac
Posted: Sat May 31, 2003 4:02 pm
by j0ker21m
I've looked into sessions. And for the most part, this solves the problem I'm having. However, I cant get sessions to work on my IBForums section of the website. Cant find where I add sessions to get it to work. However this isnt a big concern to me. Im new to php and have recently tried my hand at scripting. I appreciate all the help you guys have given on this simple script of mine. I have one more real quick question on this script. I would like the above script to not log my IP. I know it would be similar to:
Code: Select all
if ($ip == xxx.xxx.xxx.xxx) {exit;}
but I've tried all different ways of writing it, with no prevail. Also, I cannot figure how to say that if that statement=false, then to continue with the script. Any suggestions?