GetDotted Domains

Freeola Guides


Adding a Visitor Counter Using SQL

Last updated on by InFlamesForEver

Welcome to the Freeola Internet customer support pages. This guide is designed to help with setting up a visitor counter for your website. For more internet help topics please visit our main Support Page.

There are plenty of ways in which to create a visitor counter, this guide will demonstrate a method using an SQL Database and cookies to create a visitor counter that logs each unique visitor.

This method creates a visitor counter that uses cookies to detect whether a visitor has visited your website before, allowing for a more accurate visitor count than just logging whenever someone clicks on a page of your website, which can create an artificially high visitor count.

This guide assumes that your domain name is connected to a VIP Hosting with SQL service, and you have some knowledge of creating web files using a Plain Text Editor.

Please Note:

Once you have completed this guide, your website will make use of cookies, be aware that you are legally obliged to inform your users that you are using cookies.

  1. The first step is to create a new table in your SQL database. To do this, please log into your MyFreeola account and navigate to the My Web Sites section using the left-hand side navigation menu. Select the Options & FTP Settings for your website and scroll down to the MySQL Databases section of the VIP server settings and select View Settings.

  2. In the MySQL section, if you have an existing database open it up by clicking on the Manage icon. If you don't already have a database, create a new one by clicking the Add New Database button and then giving the database a name and a password.

    After pressing the Manage icon you should be in the phpMyAdmin control panel. Using the left hand side menu select the database you have created and create a new table. The name can be your choice, but the number of fields should be set to 1.

    On the next screen the following should be entered and the rest left as default:

    • Field: The name of the field, this is up to you.

    • Type: This should be set to Int.

    • Default: This should be set to 0.

    The last part of editing the database is inserting a row into the table, to do this press on the Insert tab. For a single field, make sure the value is 0, and then press Go.

    The database set-up is now complete.

  3. Now that you have a new table in your database with one field in it, the next step is to create a new PHP file to connect to this. To do this create a file within your text editor called db_connect.php.

    Within this file please place the following code:

        $hostname = "localhost";
        $database = "your_database_name";
        $username = "your_database_name";
        $password = "your_database_password";
        $conn = mysqli_connect($hostname, $username, $password, $database);
    	// Check connection
    	if (mysqli_connect_errno())
    	  echo "Failed to connect to MySQL: " . mysqli_connect_error();		

    Please Note:

    The assigned data for the $database, $username, and $password variable assignments needs to be changed to your database information. For instance if your database is called "h001_test" your $database and $username will be "h001_test".

    This code creates a connection to the database, and checks that the connection was established without error, if an error is detected it is displayed on the webpage.

  4. With a way to connect to the database, next you need to create a file that can make use of this, create a file called visitor_counter_cookie.php.

    Within this file please place the following code:

    	$cookie_name = "visCounter";
    	if(!isset($_COOKIE[$cookie_name])) {	
    		$sql_Query = "UPDATE hit_counter SET count=count+1";
    		if ($conn->query($sql_Query) === FALSE) 
    			echo "Error updating record: " . $conn->error;
    			setcookie($cookie_name, "is counted", 
    			time() + (10 * 365 * 24 * 60 * 60), "/"); // expires in 10 years

    Please Note:

    If you did not use the name dbconnect.php for the first file, you will need to edit this to the file name that you chose.

    This code checks to see if the visitor has visited the website before, to do this it checks if the users browser has the "visCounter" cookie set in it. If they have visited before nothing is done, however if they haven't, the visitor counter is increased by 1 in the database and a cookie is created in the visitors browser so that they are not counted the next time they visit.

  5. The last file that you have to create is a file to display the visitor counter on your website, this file should be called counter.php.

    Within this file please place the following code:

    	$quer = mysqli_query($conn,"SELECT count from hit_counter");
    	echo "Visitor Count: " . $quer->fetch_array(MYSQLI_NUM)[0];

    This file gets the amount of visitors from the database and then displays it on the page.

  6. As all of the files have now been created, the last part is to add the visitor counter to your web pages. To do this, add the following code to the header of any web page that you want the visitor counter to count.


    This code will import the code from the visitor_counter_cookie.php page on each web page that it is added to.

    To display the visitor counter on your web page, add the following code to the body of each web page you want to display the counter on.


Did you find this article helpful?

Freeola & GetDotted are rated Five stars

Check out some of our customer reviews below:

View More Freeola Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, Edge, or Opera will provide a better, safer browsing experience for you.