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 2 of 2

Thread: Visit Counter

  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Visit Counter

    PHP Code:
    <?php
    if($_SERVER['REMOTE_ADDR']) {

    $host "localhost";
    $username "root";
    $pass "";

    $con mysql_connect($host$username$pass);
    if(!
    $con) {
    die(
    'Server Error: ' mysql_error());
    }

    mysql_select_db("site"$con);

    $ip $_SERVER['REMOTE_ADDR'];

    $sql mysql_query("SELECT ip FROM visits WHERE ip='$ip'") or die("Server Error: " mysql_error());

    $check mysql_num_rows($sql);

    if(
    $check['ip'] >= 1) {

    $fetch mysql_query("SELECT amount FROM visits WHERE ip='$ip'") or die('Server Error: ' mysql_error());

    $num mysql_fetch_assoc($fetch);

    $visited $num['amount'] + 1;

    mysql_query("UPDATE visits SET amount='$visited'") or die('Server Error: ' mysql_error());

    }else {

    $new mysql_query("INSERT INTO visits (`ip`,`amount`) VALUES('$ip','1')") or die("Server Error" mysql_error());

    }

    }

    ?>
    Basically I'm trying to build a counter that will count how many times that person with that ip has came to one of my pages. My problem is that whenever I refresh the page its adding another column in the database and not taking the already existing number and updating that number thats in the database by one. So I wonder if you guys could help me out on this. I'm sure its something simple that I'm overlooking. Thanks for the help guys.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    $check mysql_num_rows($sql);

    if(
    $check['ip'] >= 1) { 
    Your variable $check is not an array; it's an integer. Use it without the index reference.


  •  

    Posting Permissions

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