Announcement

Collapse
No announcement yet.

php/mysql hit counter??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
    Slynderdale
    Member

  • Slynderdale
    replied
    Heres one I quickly made for a freind, it uses no cookies and so on, it stores the useragent and host of the person instead. You can also esily reset the counter any time you want by going to:
    http://yoursite/counter.php?action=r...sword=password

    Also if some one tries to access the counter file, they'll get a 404 error. Also the counter wont count the person accessing the counter file itself.

    How to use the script on your site:
    All you have to do is include/require the counter file any wheres inside the site where you want to keep track of the hits. For example, if you have a php file all you have to do is add include('counter.php'); just below the <?.

    If your index/or the page you wanna keep track of hits is an html file, change it to file.php (can be what ever you choose) and at the top have <? include('counter.php'); ?>, you can leave the rest of the html intact.

    Instead of printing the hit data directly out, the data is stored within varibles so you can esily customise the looks. Just place the varible any wheres you want for example:

    echo "Total Hits: $counter_total";

    or if you renamed a html file to php as stated before, you can use:

    <? echo "Total Hits: $counter_total"; ?>

    The password for the reset can be what ever you want, theres a setting inside a file where you can set it.

    Heres what data this stores about the person:
    Host (ip)
    Reseloved Host
    User Agent
    Port
    Referer
    Timestamp

    So if you wanted to could esily make a script that accesses this data for example for a list of referering sites and so on.

    Heres what varibles the counter uses and what they are used for:

    $counter_twentyfour - Gets the hits in the last 24 hours
    $counter_total - Gets the total hits
    $counter_today - Gets the total hits today
    $counter_uniquetwentyfour - Gets the unique hits in the last 24 hours
    $counter_uniquetotal - Gets the unique total hits
    $counter_uniquetoday - Gets the unique today hits
    $counter_lasthit - Gets the date and time of the last hit
    $counter_start - Gets the date and time of when the counter started

    Heres the query you need to run:

    CREATE TABLE hit_data (
    hitid int(10) unsigned NOT NULL auto_increment,
    host varchar(20) NOT NULL default '',
    resolvedhost varchar(100) NOT NULL default '',
    useragent char(100) NOT NULL default '',
    port int(10) unsigned NOT NULL default '0',
    referer varchar(255) NOT NULL default '',
    timestamp int(10) unsigned NOT NULL default '0',
    PRIMARY KEY (hitid)
    ) TYPE=MyISAM;

    Note: The query and this info being posted are already located in the PHP file for future refrence.

    All you have to do is edited a few settings and upload and thats it, heres the settings:

    // Mysql Data
    $counter_server = "localhost";
    $counter_database = "root";
    $counter_user = "root";
    $counter_password = "";
    $counter_usepconnect = false;
    //------
    $script_name = "counter.php"; //The actual script name of the counter
    $script_password = "default"; //Password used to reset the counter
    $date_time_format = "F j, Y, g:i:s a"; //The date format for the counter
    // See http://www.php.net/date

    If you have any questions or comments or need help feel free to post them.


    Attached Files

    Leave a comment:

  • Icheb
    Senior Member

  • Icheb
    replied
    Then it still is a counter for unique visitors, not hits.

    And MySQL doesn't care if it has to handle one query more or less.

    Leave a comment:

  • cka
    Senior Member

  • cka
    replied
    Nah, just sets a 2 hour interval on each users counter session so they can't F5 all day and crash MySQL. And yeah, I probably should have used mysql_free_result too, but that was about 3 minutes' worth of typing.

    Leave a comment:

  • Icheb
    Senior Member

  • Icheb
    replied
    Actually you should use

    mysql_free_result($query)

    instead of just unset()ing it.

    And if I understand your code correctly, you don't want to count multiple hits within a certain from one user? Then you are counting visitors, not hits.

    Leave a comment:


  • Poggyuk
    replied
    Originally posted by cka
    MySQL:

    CREATE TABLE sitehits (hits int unsigned not null default '1');

    PHP:
    PHP Code:
    <? // counter.php
    if (!isset($_GET['show']) || $_COOKIE['cookietracker'] != 1) {
    $conid mysql_connect("host""user""pass") or die("Can't connect to database");
    mysql_select_db("your_database");
    $query mysql_query("UPDATE sitehits SET hits=hits+1");
    mysql_close($conid); unset($query);
    setcookie("countertracker",1,time()+7200);
    } else {
    $query=mysql_query("SELECT hits FROM sitehits");
    $result mysql_fetch_row($query);
    echo 
    $result['hits'];
    }
    ?>
    Then on your pages, simply include("counter.php");. To show # of hits, include("counter.php?show=1");

    It's fast, dirty, and probably won't work right as-is, but there's the cheapest counter you can get with SQL/PHP.
    Thanks, i'll give it a go !

    Leave a comment:

  • cka
    Senior Member

  • cka
    replied
    MySQL:

    CREATE TABLE sitehits (hits int unsigned not null default '1');

    PHP:
    PHP Code:
    <? // counter.php
    if (!isset($_GET['show']) || $_COOKIE['cookietracker'] != 1) {
    $conid mysql_connect("host""user""pass") or die("Can't connect to database");
    mysql_select_db("your_database");
    $query mysql_query("UPDATE sitehits SET hits=hits+1");
    mysql_close($conid); unset($query);
    setcookie("countertracker",1,time()+7200);
    } else {
    $query=mysql_query("SELECT hits FROM sitehits");
    $result mysql_fetch_row($query);
    echo 
    $result['hits'];
    }
    ?>
    Then on your pages, simply include("counter.php");. To show # of hits, include("counter.php?show=1");

    It's fast, dirty, and probably won't work right as-is, but there's the cheapest counter you can get with SQL/PHP.

    Leave a comment:

  • Floris
    Senior Member

  • Floris
    replied
    If your site has cpanel, it comes with pre-installed scripts, one of them is a counter. And otherwise those above sites are great to find good counter scripts, and if not, then there is always freshmeat.net

    Leave a comment:

  • JAWilliams
    Senior Member

  • JAWilliams
    replied
    Originally posted by Poggyuk
    Hi all,

    Does anyone know a script / how to make a SIMPLE hit counter that stores its data (hits) in an sql database?

    Thanks in advance if you can help!

    try http://php.resourceindex.com
    or http://www.hotscripts.com

    there must be something like it on one of them

    Leave a comment:


  • Poggyuk
    started a topic php/mysql hit counter??

    php/mysql hit counter??

    Hi all,

    Does anyone know a script / how to make a SIMPLE hit counter that stores its data (hits) in an sql database?

    Thanks in advance if you can help!
widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
Working...
X