Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    116
    Thanks
    2
    Thanked 0 Times in 0 Posts

    how to put a PHP hit counter

    HI friends,

    I have two issues

    I want to put a

    1) website visit counter in my website
    2) A download hits counter in my download page

    I haven't used PHP to design my website, only HTML and javascript.

    I think it's not possible to have these features using a client side script like javascript, and I think PHP would solve my problem.

    but how can I run a PHP script in a HTML page, is it possible?
    what would be the best way so that I will need to learn PHP?

  • #2
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    Well i would add a field in my MYSQL database called views, i would then edit my main page and add something like this :

    PHP Code:
    <?
    $sql
    ="SELECT * FROM views";
    $result=mysql_query($sql);

    $rows=mysql_fetch_array($result);

    $views=$rows['views'];

    $views2$views+1;

    $sql2="UPDATE views set views='$views2' where views='$views'";
    $result2=mysql_query($sql2)

    echo 
    $views;

    ?>
    Hope this helps!

    EDIT : sorry not INSERT, update
    Last edited by Killermud; 03-18-2009 at 07:21 PM.

  • #3
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    If you're just starting out with PHP perhaps it would be easier to do counter using simple text file.

    Save following code as counter.php

    PHP Code:
    <?php 
    // Open file for reading
    $file fopen('count.txt''r');
    // Read file
    $data fread($file1024);
    // Close file
    fclose($file);

    // Open file for writing
    $file fopen('count.txt''w');
    // Write new visitor count increased by 1
    fwrite($file$data+1);
    // Close file
    fclose($file);

    // Show visitor count on the page
    echo $data;
    ?>
    Also create new empty file called count.txt, it will hold your visitor count. Place both files to directory where you web page is located. Rename you web page extension from .html to .php and add this code where you like visitor count to appear:

    PHP Code:
    <?php include('counter.php'); ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •