Creating a page view counter in PHP and MySQL

Discussion in 'Archives' started by lumentec_dark, Nov 9, 2008.

Creating a page view counter in PHP and MySQL
  1. Unread #1 - Nov 9, 2008 at 4:24 PM
  2. lumentec_dark
    Joined:
    Nov 9, 2008
    Posts:
    52
    Referrals:
    0
    Sythe Gold:
    0

    lumentec_dark Member
    Banned

    Creating a page view counter in PHP and MySQL

    Firefox crashed the first time I wrote this so I had to re-do it. :'(



    Creating a page view counter in PHP and MySQL


    This short guide will allow you to add a counter to your pages. In order for this counter to work, you will need:

    • A professionally hosted website
    • MySQL

    If you lack either of those things (meaning you have a free hosted website, or something besides MySQL) then do not continue on with this guide, as it will be no help to you.

    Beyond that, you will be able to create your counter. The rest of this guide is useful for you only if you have:

    • CPanel
    • MySQL Administration in CPanel
    • PHPMyAdmin

    If you lack any of those things, contact me so I can write you a script which will take the place of this guide. If you don't know if you have these things, follow steps 1 and 4 to find out. If you cannot find any of the things in steps 1 and 4, you do not meet the requirements.


    If you've met all the requirements so far, follow these steps to create your counter.




    PLEASE NOTE: You may click any picture from this point on to see a larger view.





    Step 1: Locating your MySQL administration

    1. Open up CPanel.
    2. Locate the MySQL administration icon, which should look something like this:

    [​IMG]

    3. Click that icon.



    Step 2: Creating your database

    1. Locate this box:

    [​IMG]

    2. Type into the box "hits" and hit the "Create Database" button.

    3. When you create your database, a prefix may be added. To makes sure you know the real name of your database, look for it in the list here:

    [​IMG]

    4. Write down the name of your database.



    Step 3: Creating a user and adding it to the database

    1. Locate this box:

    [​IMG]

    2. Type in a username and password to use.

    3. Hit the "Create User" button.

    4. Locate the "Current Users" right above the boxes you typed the username and password into. Check the username, because a prefix may have been added.

    4. Write the username and password down.

    5. Locate the "Add users to your databases" section and configure it like this:

    [​IMG]

    6. Hit the "Add User to Database" button.



    Step 4: Locating PHPMyAdmin

    1. Find something that looks like this:

    [​IMG]

    2. Click that link.



    Step 5: Adding a Table

    1. Find your database on the left side and click it. It should look something like this:

    [​IMG]

    2. Find this and type in "hits" for the name and "2" for number of fields:

    [​IMG]

    3. Configure it like this:

    [​IMG]

    Bookmark 1

    4. Now go click this:

    [​IMG]

    5. Set it up like this and click the "Go" button in the right below the two fields you filled out, and make sure you have the "ignore" box checked like shown in the picture:

    [​IMG]

    END BOOKMARK



    Step 6: Adding the Counter

    1. Make your page a .php file instead of a .html or .htm if you havn't done so already. (For some people this may require you to either re-upload the file as a .php or just to rename the file.)

    2. Add the following code anywhere inside the <body> tag where you want your counter number to appear replacing all instances of USERNAME, PASSWORD, and DATABASE. If you configured this counter to be for a different page than the home page, replace all instances of "home" with your page name, as well. :

    Code:
    <?php
    $connection = mysql_connect("localhost","USERNAME","PASSWORD");
    
    mysql_select_db("DATABASE",$connection);
    
    $result = mysql_query("SELECT * FROM hits WHERE page='home'") or
    die(mysql_error());
    
    $row = mysql_fetch_array($result);
    
    $temp = $row["number"]+1;
    
    mysql_query("UPDATE `DATABASE`.`hits` SET `number` = '$temp' WHERE CONVERT( `hits`.`page` USING utf8 ) = 'home' LIMIT 1");
    
    echo $temp;
    ?>
    
    NOTE: If you'd like to add this to multiple pages in which each page has its own counter, repeat Bookmark 1 and Step 6 with a different page name.



    THAT'S IT, now you've got a working page view counter!
    I hope this guide helped, it took me a quite a while to complete. :)



    This guide © 2008 Lumentec_Dark. All rights reserved.

    [​IMG]
    ------------------------------
    -------------------------
    --------------------
    ---------------
    ----------
    -----
    -​
     
  3. Unread #2 - Nov 9, 2008 at 4:50 PM
  4. Benwise
    Joined:
    Sep 20, 2007
    Posts:
    5,768
    Referrals:
    6
    Sythe Gold:
    0

    Benwise Hero

    Creating a page view counter in PHP and MySQL

    WOW.. Very informative, great pictures, explained each step. I have no idea what it is, but seriously, Im guessing this would be a very easy guide to follow because of its clarity.

    10/10.
     
  5. Unread #3 - Nov 9, 2008 at 5:41 PM
  6. lumentec_dark
    Joined:
    Nov 9, 2008
    Posts:
    52
    Referrals:
    0
    Sythe Gold:
    0

    lumentec_dark Member
    Banned

    Creating a page view counter in PHP and MySQL

    Thanks. :p

    It took about 5 hours and this is a guide to add a page view counter to a web page (a numbers shows how many times that web page has been viewed.) which is completely controlled by you (as apposed to some third party thing with all kinds of embedded copyrights, links, and advertisements).
     
  7. Unread #4 - Nov 9, 2008 at 11:47 PM
  8. jd 365
    Joined:
    Feb 20, 2007
    Posts:
    1,201
    Referrals:
    0
    Sythe Gold:
    0

    jd 365 Guru
    $5 USD Donor

    Creating a page view counter in PHP and MySQL

    Very nice guide =) I give you kudos for this, dont think ill ever use it but o well it was a nice guide to read.
     
< The Basic Guide on Respect | selling pure ranger! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site