PDA

View Full Version : | PHP Stats - Tutorial |



Dan Williamson
18-12-2005, 11:20 PM
Hey guys.

Heres my 4th PHP tutorial just rate it and tell me what you think.

We'll be going over user information and statistics.

We'll be going through loads.

Users IP.

Too show the users IP adress


<?php echo $_SERVER["REMOTE_ADDR"]; ?>


Very simple, one line of code just place it in your table or wherever you want the IP adress to show.

An advanced and better way of getting someones IP adress


<?php

if(getenv(HTTP_CLIENT_IP)) {
$host = getenv(HTTP_CLIENT_IP);
}
elseif(getenv(HTTP_X_FORWARDED_FOR)) {
$host = getenv(HTTP_X_FORWARDED_FOR);
}
else {
$host = $REMOTE_ADDR;
}

?>


Browser Info

This is another easy one line code.


<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>


Very basic and easy too use.

Referer

Heres how too see what site they came from.



<?php echo $_SERVER["HTTP_REFERER"]; ?>

Hit Counter

Now there a lot of different hit counters out there.

We're going to be using one like i'm using on my site, one thats run off a text file.

Now lets create a blank .txt file called count.txt

Now heres the simple PHP code.



<?php

$fp = fopen("count.txt","r");

$count = fread ($fp, filesize
("count.txt"));

fclose($fp);

$count++;

$fp = fopen("count.txt","w");

fwrite($fp, $count);

fclose($fp);

echo "Hits : $count";
?>


Now you need to CHMOD the text file too 777 if you don't know how to do that then you shouldn't use this tutorial.

This is my 4th tutorial and they will all be used on my upcoming site opening 1st January 2006

- Dan

Splinter
19-12-2005, 08:06 AM
Cool :D but as far as i know the more advanced ip should be =

<?php

if(getenv(HTTP_CLIENT_IP)) {
$host = getenv(HTTP_CLIENT_IP);
}
elseif(getenv(HTTP_X_FORWARDED_FOR)) {
$host = getenv(HTTP_X_FORWARDED_FOR);
}
else {
$host = $_SERVER['REMOTE_ADDR'];
}

?>

since in the new version of php $_SERVER['REMOTE_ADDR']; replaces $REMOTE_ADDR ...

TwoWorlds
19-12-2005, 09:09 AM
this will help alot of the newbies who keep asking about these codes! Well done.
+rep :P

Rix
19-12-2005, 10:29 AM
i've been tryign to do that for ages

Dan Williamson
19-12-2005, 06:00 PM
Cheers charlie.

I may not be up and advanced on the new PHP :p

Just no that one worked for me in the past.

- Dan

Splinter
19-12-2005, 07:04 PM
Depends if your host has installed the latest php version or not.. :P

someperson
20-12-2005, 05:41 PM
Coooool

Want to hide these adverts? Register an account for free!