<?

/*
This counter prints the number of visitors and the number of distinct visitors.

This database table for this counter stores the IP address of the visitor and the formatted 
date at which the visit took place.

Note that to use this counter, you need to include the following statement in
the "php" page in which you want to include the counter:
include("counter_v1.php");
The page in which you include this has to be a PHP page. You'll have to assign a value for
the variable $page in counter_multi_pages.php in the include file (not the included one). But,
you can have an htaccess file to make it work in other types
of documents. You can also use server side includes (SSI) commands
and include it in an HTML file. In this case, make the extension of the HTML
file .shtml (instead of .html or .htm) and include the following statement in
the place where you want the number of visitors to be printed:

<!--#include virtual="counter_v1.php"-->

If the file you're including is located in another directory, you can use it as follows:
E.g. assume that the PHP file is located in www/php and the shtml file in which you're including
the PHP file is located in www/teaching/Fall_06/121

<!--#include virtual="../../../php/counter_v1.php"-->

*/

$ip = $_SERVER['REMOTE_ADDR'];
$date = date('j F Y, H:i:s T');

@$d = mysql_connect('localhost','database_username','database_password') or die("Can't connect");

@mysql_select_db('counter') or die();

$q = "insert into counter_v1 (ip,date) values (\"$ip\",\"$date\")";
@mysql_query($q) or die();

/*
$q = "select ip from counter_v1";
@$r = mysql_query($q) or exit();
$n = mysql_num_rows($r);
echo $n, " visitors.<BR>";
*/

$q = "select count(ip) from counter_v1";
$r = @mysql_query($q) or exit();
$row = mysql_fetch_row($r);
echo "<center>", $row[0], " visitors (";

/*$q = "select distinct ip from counter_v1";
@$r = mysql_query($q) or exit();
$n = mysql_num_rows($r);
echo $n, " distinct visitors.<BR>";
*/

$q = "select count(distinct ip) from counter_v1";
$r = @mysql_query($q) or exit();	
$row = mysql_fetch_row($r);
echo $row[0], " distinct visitors) since Oct 29, 06.</center><BR>";

mysql_close($d); 
?>
