<?

/*
This counter prints the number of visitors to a page. It can be used for any page 
on the website with no changes.

The database table that stores the data has 3 columns: name (this is the name 
of the homepage that needs a counter; in this case, I pass the part of its URL that 
starts after the www directory as a name in order to get a unique name and in order 
for the counter to know what homepage it's counting because it can be used for any 
number of homepages), the number of visits, and the date (formatted) at which the 
homepage was first visited. What I do is I check first if the visited homepage has
a record (row) in the table (i.e. visited before). If yes, I increment its counter. 
If not, I start the count and insert the page in the database. I made the name column
a primary keep because I want each homepage to have just one row in the table.

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_multi_pages.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 because the default file type for SSI commands is shtml but you can allow 
other types of files to execute SSI commands by modifying the .htaccess file) and include the following statement in 
the place where you want the number of visitors to be printed:

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

If you want to include SSI commands such as the above in files with extensions such as .html or .htm or others, you 
need to allow SSI commands to be executed in such types of files by modifying the .htaccess file.

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_multi_pages.php"-->

or

<!--#include virtual="/abu-jeib/php/counter_multi_pages.php"-->

Note that the following will not work:

<!--#include virtual="abu-jeib/php/counter_multi_pages.php"-->
<!--#include virtual="/abu-jeib/www/php/counter_multi_pages.php"-->
<!--#include virtual="/home/abu-jeib/php/counter_multi_pages.php"-->
<!--#include virtual="/home/abu-jeib/www/php/counter_multi_pages.php"-->

To pass parameters to the included PHP file (in this case, you need to pass the URL - of the 
homepage that needs a counter - that starts after the www directory), you do 
it as follows:

<!--#include virtual="../../../php/counter_multi_pages.php?Name=teaching/Fall_06/121/index.shtml"-->

To pass more than one variable, you do it as follows:

<!--#include virtual="../../../php/counter_multi_pages.php?parameter1=value1&parameter2=value2"-->

*/

// First get the homepage you want to process its counter
$page = trim(stripslashes($_GET['Name']));

// Check just in case someone accessed this counter from no where (i.e.
// without passing the name of the homepage that needs a counter
if (empty($page)) 
        exit;

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

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

$date = date('D d, M, Y H:m:i');

$q = "select * from counter_multi_pages where name = \"$page\"";
$r = @mysql_query($q) or exit("none");
	
$row = mysql_fetch_row($r);
if ($row[1] == "" || $row[1] == "NULL" || is_null($row[1]))
{
	$count = 1;
	$q = "insert into counter_multi_pages (name,count,date) values ('$page','$count','$date')";
	@mysql_query($q) or die();
	echo "<center>", $count, " visitors since $date.";
}
else
{
	$count = $row[1] + 1;
	$q = "update counter_multi_pages set count=$count where name='$page'";
	@mysql_query($q) or die();
	echo "<center>", $count, " visitors since $row[2].";
}

mysql_close($d); 
?>
