WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Making a simple Hit-Log using PHP and MySql
Categories : PHP, Log Files, Beginner Guides, Databases, MySQL Click here to Update Your Picture
P C
Date : Apr 18th 2005
Grade : 2 of 5 (graded 2 times)
Viewed : 13823
File : No file for this code example.
Images : No Images for this code example.
Search : More code by P C
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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.



Convert a File database into MySQL
Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides
for each record, do this to the first record, and do that to any subsequent record
Categories : PHP, Databases, MySQL, Beginner Guides
Newbie Notes #4 - Trapping dumb MySQL query errors
Categories : PHP, Databases, MySQL, Debugging, Beginner Guides
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
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
Newbie Notes #10 - Generating drop downs
Categories : PHP, MySQL, HTML, Beginner Guides, Databases
How to Insert a Date Format Into MySQL from PHP
Categories : PHP, Databases, MySQL, Date Time, Beginner Guides
Add, Edit /Update & Delete all in one Contact Management Form
Categories : PHP, MySQL, Databases, Beginner Guides
Cut your MySQL Connections to 1 line of code
Categories : PHP, Beginner Guides, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
PHP Calendar Web App
Categories : PHP, Databases, MySQL, Date Time, Calendar
A Simple sign up script with PHP and JavaScript validations.
Categories : PHP, Java Script, MySQL, Databases
Phorum, MySQL, Language, UK date format, MySQL UK Date format
Categories : PHP, Date Time, Strings, MySQL, Databases