• home
  • forum
  • my
  • kt
  • download
  • Create a Unique Hits Counter Using PHP and MySQL

    Author: 2008-08-28 09:44:41 From:

    Ok, let's start with the SQL. Put this into your phpMyAdmin after you have made the database & the user.

    CODE

    CREATE TABLE supporters_ips(
    id VARCHAR( 15 ) NOT NULL,
    ip VARCHAR( 100 ) NOT NULL
    );

    CREATE TABLE supporters(
    id INT( 11 ) NOT NULL AUTO_INCREMENT ,
    PRIMARY KEY ( id ) ,
    site_adress VARCHAR( 100 ) NOT NULL ,
    site_title VARCHAR( 100 ) NOT NULL ,
    hits VARCHAR( 100 ) NOT NULL DEFAULT '0'
    );


    That creates the tables.

    Now for supporters_config.php

    CODE

    <?php
    $db_user = "YOUR USERNAME";
    $db_pass = "YOUR PASS";
    $db_host = "localhost";
    $db_name = "YOUR DATABASE";

    $mysql_access = mysql_connect($db_host, $db_user, $db_pass);
    mysql_select_db($db_name);
    ?>


    That just connects to the database.

    Now view_hits.php this is your admin panel to view the number of hits that the site sends to you, and to add new sites.

    CODE

    <body bgcolor="#000000">
    <b>
    <center><table width="500" border="1" bgcolor="#2A7FFF">
    <tr>
    <td width="66%"><center>
    <b><u>Site Name:</u></b>
    </center></td>
    <td width="33%"><center>
    <b><u>Number of sent hits</u></b>:
    </center></td>
    </tr>
    <?php
    include 'supporters_config.php';

    //select the table
    $result = mysql_query("select * from supporters");

    //grab all the content
    while($r=mysql_fetch_array($result))
    {
    $site_title=$r["site_title"];
    $site_address=$r["site_adress"];
    $number_of_hits=$r["hits"];
    $id=$r["id"];
    //display the row in a table
    echo "<tr>
    <td width=\"66%\"><center><b><a href='$site_address' target='_blank'>$site_title</a></b></center></td>
    <td width=\"33%\"><center><b>$number_of_hits</b></center></td>
    </tr> ";
    }
    ?>
    </table>
    <p>
    <a href="add_new.php">Add a new site</a>
    </center>
    </b>
    </body>


    Now add_new.php, this adds a new site (what a suprise).

    CODE

    <body bgcolor="#030003">
    <b>
    <center>
    <table border="0" bgcolor="#2A7FFF">
    <tr>
    <th scope="row"><b>
    <?php
    include 'supporters_config.php';
    if($_POST['submit']){
    $site_name = $_POST['site_name'];
    $site_address = $_POST['site_address'];

    if ($site_name == ""){
    echo "One or more fields are left blank.";
    }
    elseif($site_address == ""){
    echo "One or more fields are left blank.";
    } else {
    $sql=mysql_query("INSERT INTO `supporters` ( `site_title` , `site_adress` ) VALUES ('$site_name', '$site_address')");

    $result = mysql_query("select * from supporters limit 1");

    //grab all the content
    while($r=mysql_fetch_array($result))
    {
    $id=$r["id"];
    //display the row
    echo "Congratulations, you have successfully added a new site. That new site's id is $id.<p><a href=\"view_hits.php\">Go back?</a>";
    }
    }
    } else {
    ?>
    <form action="?submit=true" method="post" name="form1" id="form1">
    <label>Site Name:
    <input type="text" name="site_name" />
    </label>
    <p>
    <label>Site Address:
    <input type="text" name="site_address" />
    </label>
    </p>
    <p>
    <label>
    <input type="submit" name="submit" value="Submit" />
    </label>
    </p>
    </form>
    <?php
    }
    ?>
    </b></th>
    </tr>
    </table>
    </center>
    </b>
    </body>



    Now in.php, this page is what counts the incoming hits.

    CODE

    <?php
    ob_start();
    include('supporters_config.php');
    $id=$_GET['id'];
    if (is_numeric($id)) {
    $ip = $_SERVER['REMOTE_ADDR'];

    $sql22 = @mysql_query("SELECT * FROM supporters_ips where id = '$id' and ip = '$ip'");
    $unique_visitors = @mysql_num_rows($sql22);
    if ($unique_visitors == 0){


    $sql2=mysql_query("INSERT INTO `supporters_ips` ( `id` , `ip` ) VALUES ('$id', '$ip')");
    $sql=@mysql_query("SELECT * FROM supporters WHERE id = $id LIMIT 1");
    $r=@mysql_fetch_array($sql);
    $hits=$r['hits'];
    $hits++;
    mysql_query("UPDATE supporters SET hits = '$hits' WHERE id = '$id'");
    //Everything is successfull!
    header("Location: index.html");
    } else {
    header("Location: index.html");
    }
    } else {
    header("Location: index.html");
    }
    ob_end_flush();
    ?>


    Remember to change the header("Location: index.html") to header("Location: YOUR_SITE'S_HOMEPAGE").

    Well that is about the whole tutorial. Please try to leave credit, as this took time to write.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (9)
      Database Related (20)
      Date and Time (13)
      Development (19)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot