Results 1 to 13 of 13

Thread: How to put number of visitors till date in PHP

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    How to put number of visitors till date in PHP

    Hi
    I am making a PHP poject in that i have to put counter ie. no. of visitors till date in PHP .I am not able to find exact method for that can anyone help me

    Thank You

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: How to put number of visitors till date in PHP

    How are you tracking visitors? Everytime someone requests a PHP page, you could just INSERT into a MySQL table the IP and Date or something so you can track the visitor. Or maybe write to a file.

    Then you'd just count the rows and print the number of visitors since date("m-d-Y") or something.

    We need more information about what you want
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: How to put number of visitors till date in PHP

    You can count unique visits and repeat visits. You can count repeat visits easily just bycount the number of page requests. For unique visits, place a cookie on the users computer and use it to check whether the visit has been logged in the past.

    This is a script I use:
    PHP Code:
    <?php


        $db 
    = new DB;

        if (isset(
    $_COOKIE['visit'])) {    
            
    $id = (int) $_COOKIE['visit'];
            
    $query "SELECT id FROM hits WHERE id='$id'";
            
    $result $db->query($query);

            if((
    $row = @$db->fetch_assoc($result))) {
                
    update_hit($row['id']);
            } else {
                
    create_hit();
            }
                
        } else {
            
    create_hit();
        }
        
        
    header('Content-Type: image/gif');
        
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
        
    header('Pragma: private');
        
        @
    readfile('1px.gif');
        
        function 
    create_hit()
        {
            global 
    $db;

            
            
    $last_visit time();
            
    $ip ip_to_int($_SERVER['REMOTE_ADDR']);
            
    $expire mktime(0,0,0,12,31,2007);


            
    $insert "INSERT INTO hits (ip, last_visit) VALUES ($ip$last_visit)";
            
    $db->query($insert);

            
    $id $db->last_insert_id();

            
    setcookie('visit'$id$expire);
        }

        function 
    update_hit($id)
        {
            global 
    $db;

            
    $id = (int) $id;
            
    $last_visit time();
            
    $update "UPDATE hits SET last_visit=$last_visit,visit_count=visit_count+1 WHERE id =$id";

            
    $db->query($update);
            
        }

        function 
    ip_to_int($ip)
        {
            
    $parts explode('.'$ip);
            
            
    $ip_int 0;

            for(
    $x0$x <= 3$x++) {

                
    $multiplier pow(256$x);
                
    $ip_int += ($multiplier $parts[$x]);
            }

            return 
    $ip_int;
        }
    ?>
    It requires a database with the following table:
    Code:
    			CREATE TABLE hits (
    						id INTEGER PRIMARY KEY,
    						hash VARCHAR(32) NOT NULL,
    						ip INTEGER NOT NULL,
    						last_visit INTEGER,
    						visit_count INTEGER NOT NULL);
    You also need an image called 1px.gif. Insert this into your page as a hidden image:
    HTML Code:
    <img src="1px.php" alt="counter" style="display: none" />
    If you want to show a counter, you can replace the image with a dynamically generated image. This script will produce an image containg the number you require:
    PHP Code:
    <?php

        
    /* the following headers prvent caching by the browser */
        // Date in the past
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

        
    // always modified
        
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
     
        
    // HTTP/1.1
        
    header("Cache-Control: no-store, no-cache, must-revalidate");
        
    header("Cache-Control: post-check=0, pre-check=0"false);

        
    // HTTP/1.0
            
    header("Pragma: no-cache");

        
    $number = (string) ((int)@$_GET['number']);
        
        
    /* how many digits do we need - calculate the length of the image */
        
    $imageLength strlen($number) * 16;

        
    /* create an empty image, big enough to hold the required number of 16x24 digits */
        
    $image imagecreate($imageLength24);

        
    /* an empty array to store already loaded image resources */
        
    $img = array();
        
        
    /* loop through each character in the string of numbers */
        
    for($x 0$x strlen($number); $x++) {
            
    /* get the number, locate its position in the big image */
            
    $no substr($number$x1);
            
    $xStart = ($x==0)?0:($x 16) + 1;
            
            
    /* if this number isn't already loaded, load it */
            
    if(! is_resource(@$img[$no])) {
                
    $img[$no] = @imagecreatefromgif("num/$no.gif");
            }
            
            
    /* add the appropriate digit to the big image */
            
    imagecopy($image$img[$no], 
                    
    $xStart,0,0,0,16,24);
        }

        
    /* image - flush to the browser */
        
    header('Content-Type: image/gif');    
        
    imagegif($image);

    ?>
    You need to have the GD extension enabled to use it. I've a attached the numbers as a download. The link is constructed with a number in the querystring, so:

    number.php?number=1234567890

    Will produce:



    I've attached the image generation script and the image files containing the numbers.
    Attached Files Attached Files
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: How to put number of visitors till date in PHP

    I have a very very simple script going on my index page. Every load it opens and increments a text file. this is not the most accurate since simply reloading the page will increment it. I plan to update it to a better tracker later but its a very basic one, not that much code.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: How to put number of visitors till date in PHP

    Quote Originally Posted by kasracer
    How are you tracking visitors? Everytime someone requests a PHP page, you could just INSERT into a MySQL table the IP and Date or something so you can track the visitor. Or maybe write to a file.

    Then you'd just count the rows and print the number of visitors since date("m-d-Y") or something.

    We need more information about what you want
    Hi
    Thanks for your great response!

    what exactly i want is that as the website page opens no. of visitors increases by one by on refreshing it should not increase .
    Any such file is there in php that call at one time only .

    I think it is not an good idea to keep track of visitors in dbase as dbase will soon become very heavy ? What do u think ? Give me some other options

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: How to put number of visitors till date in PHP

    Since you want to make it so hitting refresh doesn't increase the number of hits, you'd have to keep track of IP addresses. You could try added all of these visits into a text file but that would become large and slower to parse. It would probably be most efficient to use a database and store the visits and IP addresses and don't add anything to the database if the IP address already exists.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: How to put number of visitors till date in PHP

    Quote Originally Posted by sinyal_varsha
    Hi
    Thanks for your great response!

    what exactly i want is that as the website page opens no. of visitors increases by one by on refreshing it should not increase .
    Any such file is there in php that call at one time only .

    I think it is not an good idea to keep track of visitors in dbase as dbase will soon become very heavy ? What do u think ? Give me some other options
    An IP address is unreliable. You need to use a cookie. Look at my previous reply.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: How to put number of visitors till date in PHP

    Cookies can be unreliable too since not all browser supports then, and not everyone have them turned on. I was in a big presentation at the UN building about how to track users and count them ++. And the big companies said they used a mix between cookies and an advanced version of the "text file" method. No matter how much time you lie in this, it will never be perfect.


    - ØØ -

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How to put number of visitors till date in PHP

    You could count sessions. Not the same as unique visitors, but it is a "unique visit" if you like. Just store a session var, and increment the visit count if the var is not present.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: How to put number of visitors till date in PHP

    Again though. Sessions rely on cookies.

    The only truely reliable way of doing it is askling users to make an accoutn and login to use the site. However, you'll then have usability issues.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: How to put number of visitors till date in PHP

    Yes, but a session times out. Like I said, you're then not counting unique VISITORS, you're counting unique VISITS. Since it's utterly impossible to count unique visitors.

  12. #12
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: How to put number of visitors till date in PHP

    This topic should be put into a debate forum.Cuz thats what its turnin into.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: How to put number of visitors till date in PHP

    Quote Originally Posted by penagate
    Yes, but a session times out. Like I said, you're then not counting unique VISITORS, you're counting unique VISITS. Since it's utterly impossible to count unique visitors.
    Sessions are a little overkill for hit counting. But with a session you can opt not to use cookies and instead append the SID constant to all your links.

    Plague, your signature offends me. Go away.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width