Tuesday, October 22, 2019

Simple Web Page Hit Counter Code Using PHP and MySQL

Simple Web Page Hit Counter Code Using PHP and MySQL Website stats provide important information to a website owner about how the site is doing and how many people visit. A hit counter counts and displays how many people visit  a webpage. The code for a counter varies depending on the programming language used and the amount of information you want the counter to collect. If you, like many website owners, use PHP and MySQL with your website, you can generate a simple hit counter for your webpage using PHP and MySQL. The counter stores the hit totals in a MySQL database. The Code To get started, create a table to hold the counter statistics. Do that by  executing this code: CREATE TABLE counter ( counter INT( 20 ) NOT NULL );INSERT INTO counter VALUES (0); The code creates a database  table named  counter with a single field also called counter, which stores the number of hits the site receives. It is set to start at 1, and the count increases by one each time the file is called. Then the new number  is displayed. This process is accomplished with this PHP code: ?php// Connects to your Database mysql_connect(your.hostaddress.com, username, password) or die(mysql_error()); mysql_select_db(Database_Name) or die(mysql_error());//Adds one to the countermysql_query(UPDATE counter SET counter counter 1);//Retrieves the current count$count mysql_fetch_row(mysql_query(SELECT counter FROM counter));//Displays the count on your siteprint $count[0]; ? This simple hit counter doesnt give the website owner valuable information such as whether the visitor is a repeat visitor or a first-time visitor, the location of the visitor, which page was visited, or how much time the visitor spent on the page. For that, a more sophisticated analytics program is necessary. Counter Code Tips Wanting to know the number of people who visit your site makes sense. When you are comfortable with the simple counter code,  you can personalize the code in several ways to work better with your website and gather the information you seek. Customize the database, table, and code to include other informationHold the counter in a separate file and retrieve it using include ()Format the counter text using regular HTML around the include functionCreate different rows on the counter table for additional pages on your website

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.