• home
  • forum
  • my
  • kt
  • download
  • Php counter with cookies

    Author: 2008-08-28 10:03:35 From:

    Big thing when you are creating counter for your web sites is to disable counting same visitors every time he/she visit your page. To avoid this you would use cookies in this case because you want do disable counting for day not session.

    What we need to do now is to create new table in our existing database or new using below sql query:

    CREATE TABLE 'stats' (
    'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    'counter' INT NOT NULL
    ) ENGINE = MYISAM ;

    INSERT INTO 'stats' ( 'id' , 'counter' ) VALUES (NULL , '0');

    After you have created new table needed for this counter we need to create new file for example: "counter.php".

    Copy & Paste code below to newly created file:

    <?php

    // connect to mysql
    $link = mysql_connect('localhost', 'write_here_mysql_username', ' write_here_mysql_password');

    // select database
    mysql_select_database('write_here_mysql_database_name',$link);

    // cookie details here
    $counted = $HTTP_COOKIE_VARS["counter"];

    // if this is first visitor then
    if($counted==""){

    // we create cookie for visitor
        setcookie("counter","counted",time()+60*60*60);

    // add counter to database
        mysql_query("UPDATE stats SET counter=counter+1");
    }

    $query=mysql_query("SELECT * FROM stats");

    while($row=mysql_fetch_assoc($query)) {
    $stats=$row['counter']; }

    ?>

    Normally now you want to show this stats somewhere... What you need to do if you want to track every visit?

    Make sure you make this:
    1.  include this file in page you want to track

    <?php include("counter.php"); ?>

    2. Print counter in part of page you want to show number of visits

    <?php print $stats; ?>

    That's all.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (10)
      Counters (15)
      Database Related (20)
      Date and Time (13)
      Development (19)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot