PHP - Simple Visitor Count Using MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 343 times.

In this tutorial we will create a Simple Visitor Count using MySQLi. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = mysqli_connect('localhost', 'root', '', 'db_count');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

tut1

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Visitor Count Using MySQLi</h3>
  17. <hr style="border-top:1px dotted;"/>
  18. <h4>Please refresh the browser to count</h4>
  19. <h3>Visitor have accessed this website <label class="text-primary"><?php include 'counter.php'?></label> times</h3>
  20. </div>
  21. </body>
  22. </html>

Creating the Main Function

This code contains the main function of the application. This code will start counting when the visitor visit your page. To do this just copy and write these block of codes as shown below inside the text editor and save it as counter.php.

  1. <?php
  2. require 'conn.php';
  3.  
  4. $query = mysqli_query($conn, "SELECT * FROM `count") or die(mysqli_error());
  5. $fetch = mysqli_fetch_array($query);
  6. $rows = mysqli_num_rows($query);
  7.  
  8. if($rows == 0){
  9. mysqli_query($conn, "INSERT INTO `count` VALUES('', '')") or die(mysqli_error());
  10. }
  11.  
  12. $count = $fetch['counts'] + 1;
  13. mysqli_query($conn, "UPDATE `count` SET `counts` = '$count'") or die(mysqli_error());
  14. echo $count;
  15.  
  16.  
  17. ?>

There you have it we successfully created a Simple Visitor Count using MySQLi. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.