MyBB Community Forums

Full Version: PHP Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a little hit counter script, but it doesnt seem to be working. When the image sis loaded it should add 1 to the value in the DB, but it is adding 2...

<?php

    $con = mysql_connect("localhost","tracke****","*********");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("tracker_stats", $con);

    $day = date("j");
    $month = date("n"); 
    $result = mysql_query("SELECT * FROM stat WHERE id = '".$month."'") or die(mysql_error());  
    
    $row = mysql_fetch_array($result, MYSQL_BOTH);


    $update = mysql_query("UPDATE stat SET `".$day."` = '".($row[$day] + 1)."' WHERE id = '".$month."'"); 
    $update2 = mysql_query("UPDATE stat SET `total` = '".($row['total'] + 1)."' WHERE id = '".$month."'"); 
    if (!$update)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_close($con);
     $im = imagecreatetruecolor(1, 1);
    $red = imagecolorallocate($im, 255, 0, 0);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagecolortransparent($im, $black);
    header( "Content-type: image/png" );
    imagepng( $im );
    imagedestroy( $im );

?> 
  

EDIT: OK, this ONLY happens when the image is loaded directly, im thinking this is a browser issue Confused
Why are you even using a MySQL database for this...? Just increment the value in a file or something. Also instead of creating a new image at every request (which will be CPU intensive) just make one in Photoshop/GIMP and serve it up using file_get_contents() and the proper header().
I don't see why people still use hit counters. I thought those things died with 90s web design.
It's only generating a 1x1 px image Toungue

I want too see how many times my sig gets viewed Big Grin
(2011-05-02, 11:26 AM)Anman Wrote: [ -> ]Why are you even using a MySQL database for this...? Just increment the value in a file or something.

Because it now records the amount on each day, so it's much easier to use SQL. It can be expanded to record IPs and unique views etc...

See: http://t.dev-network.com/graph.php?m=5
(2011-05-02, 11:51 AM)Tom K. Wrote: [ -> ]I want too see how many times my sig gets viewed Big Grin

You mean how many times your sig gets loaded. Although it's getting loaded, doesn't mean that people actually look at your sig Wink
No, but it's hard to scroll past a sig without glancing at it Smile especially if it's an image Big Grin
I can't see your signature anyway, as I have the option to remove signatures from posts enabled on my account as it slows down page loading times. Smile
Didn't I already code something for you?

If you wanted me to put the number of hits in the code just ask.
(2011-05-02, 04:19 PM)Darth Stabro Wrote: [ -> ]Didn't I already code something for you?

If you wanted me to put the number of hits in the code just ask.

Yeah, I used bits from yours, then started to remember PHP again Toungue it is far more complex than this now Big Grin it records all sorts of info Wink
do you have "turbo" mode prefetching enabled on your browser? are you looking at a page that has two of your signatures showing?
Pages: 1 2