Counter/Log?
Moderator: General Moderators
Counter/Log?
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?
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
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
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
OK..here is the code I came up with:
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/
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;
}
}
?>Here is the results page: http://jokersdomain.com/links/
Last edited by j0ker21m on Sat May 31, 2003 4:06 pm, edited 1 time in total.
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
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
May 28, 2003, 10:21 pm 200.40.165.160 http://jokersdomain.com/links/log.php
its ok.. its my IP
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)
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
try:
insted of :
Code: Select all
$page = $_SERVERї'HTTP_REFERER'];insted of :
Code: Select all
$page = $_SERVERї'REQUEST_URI'];That only returns "http://jokersdomain.com" not the page that loaded it.
- AVATAr
- Forum Regular
- Posts: 524
- Joined: Tue Jul 16, 2002 4:19 pm
- Location: Uruguay -- Montevideo
- Contact:
read sticky: viewtopic.php?t=511
you post your form and you receive the variables in the $_POST['variable']
you post your form and you receive the variables in the $_POST['variable']
Thanx.AVATAr wrote:read sticky: viewtopic.php?t=511
I was hoping to x-fer variables between pages "without" using a form. I appreciate your help though.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
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?
Code: Select all
if ($ip == xxx.xxx.xxx.xxx) {exit;}