|
|
|
Instead of simply a hit-counter, it would be nice to have a record of the time of the day your site has been visited, and by whom. This example will do just that.
It is a three part procedure that consists of
1. Make and execute a file containing the procedure to create a hit-log database at a location designated by your service provider,
2. Insert PHP code at the end of your HTML pages, but rename them to .php instead of the usual .htm or .html extension.
3. Make a procedure file that displays the report of the hit-log database.
Here are the details.
1. Make a procedure file in your home directory to create a table in a database
You will have to find out from your service provider the following information:
- the name of the MySql database server, or computer (name_of_server)
- the name of the account (account_name) and password (password)
- and the name of the database (database_name)
Insert following code in a file called MakeDB.php in your site's home directory to create a new table called "log", or any other name of your choice. If you are using someone else's account, make sure that he/she does not already have a table called "log". Execute the procedure once. In fact, you could execute it as many times as you wish, it will wipe out all the data already stored in the table, if there was any.
| <?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('name_of_server', 'account_name', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('database_name') or die('Could not select database');
return $conn;
}
function MakeDB(){
$conn=ConnectDB();
$query="drop table log;"; $results=mysql_query($query);
$query="create table log (l_index int auto_increment primary key,l_date date, l_time char(8), l_remoteIP varchar(15));";
$results=mysql_query($query);
$conn->close;
}
$conn=ConnectDB();
MakeDB();
$conn->close;
?> | |
2. Insert the following code at the end of your HTML files where you want the hits recorded, not forgetting to rename your file's extension htm or .html to .php:
| <?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('name_of_server', 'account_name', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('database_name') or die('Could not select database');
return $conn;
}
function WriteLog(){
$remoteIP=$_ENV["REMOTE_ADDR"];
$query="INSERT INTO log SET l_date=CURDATE(), l_time=CURTIME(), l_remoteIP=\"$remoteIP\" ";
$results=mysql_query($query);
}
$conn=ConnectDB();
WriteLog();
$conn->close;
?> | |
3. Getting a report of the hits.
It would be nice to view your hit-log, execute the following code that you put in a file called Report.php:
| <HTML><HEAD></HEAD><BODY>
<CENTER><H1>REPORT OF HITS</H1></CENTER>
<?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('name_of_server', 'account_name', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('database_name') or die('Could not select database');
return $conn;
}
function ShowBrief(){
echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$query='select * from log';
$results=mysql_query($query);
while ($row = mysql_fetch_array($results)) {
echo "<TR><TD>$row[0]</TD> <TD>$row[1]</TD><TD>$row[2]</TD><TD>$row[3]</TD></TR>";
}
echo "</TBODY>";
}
$conn=ConnectDB();
ShowBrief();
$conn->close;
?>
</BODY></HTML> | |
Here it is, a simple but useful code example using PHP and MySql to record your hits.
Updates to this example, if any, will be posted at
http://www.mathpath.uni.cc
and look under programming. |
|
| email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Convert a File database into MySQL Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides | | | Newbie Notes #4 - Trapping dumb MySQL query errors Categories : PHP, Databases, MySQL, Debugging, Beginner Guides | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | for each record, do this to the first record, and do that to any subsequent record Categories : PHP, Databases, MySQL, Beginner Guides | | | How to Insert a Date Format Into MySQL from PHP Categories : PHP, Databases, MySQL, Date Time, Beginner Guides | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Newbie Notes #10 - Generating drop downs Categories : PHP, MySQL, HTML, Beginner Guides, Databases | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | |
|
|
|