Counter/Log?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Counter/Log?

Post 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?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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 :wink:
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Post 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/
Last edited by j0ker21m on Sat May 31, 2003 4:06 pm, edited 1 time in total.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Post 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)
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

try:

Code: Select all

$page = $_SERVER&#1111;'HTTP_REFERER'];

insted of :

Code: Select all

$page = $_SERVER&#1111;'REQUEST_URI'];
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Post by j0ker21m »

That only returns "http://jokersdomain.com" not the page that loaded it.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Post 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?
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

read sticky: viewtopic.php?t=511

you post your form and you receive the variables in the $_POST['variable']
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Post by j0ker21m »

AVATAr wrote:read sticky: viewtopic.php?t=511
Thanx.

I was hoping to x-fer variables between pages "without" using a form. I appreciate your help though.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You should look into sessions:
viewtopic.php?t=6521
http://php.net/session

Mac
j0ker21m
Forum Newbie
Posts: 9
Joined: Wed May 28, 2003 6:50 pm

Post 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?
Post Reply