SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: page hits

  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    page hits

    just wondering how i could make a simple counter that shows how many times a page has been loaded. thx alot

    hehe im useless at this PHP lark

  2. #2
    SitePoint Enthusiast kcabobert's Avatar
    Join Date
    Aug 2001
    Location
    Topeka, KS
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I liek to use a database so I can track "extra-info" such as referrer. A database keeps iot accessable.

    A simple script would be:
    PHP Code:
    <?
        $logname 
    "site1log";
        
    $link = @mysql_connect("localhost""user""password");
        
    mysql_select_db("loggs");

        
    $querysql "SELECT * FROM $logname";
        
    $result mysql_query($querysql);

        for (
    $i 0$i mysql_num_fields ($result); $i++) {
            
    $meta mysql_fetch_field($result);
            
    $colnames["$i"] = $meta->name;
        }
        
    $thedate date("F j, Y, g:i a");
        
    $icolnames = @implode(","$colnames);
        
    mysql_query("INSERT INTO $logname ($icolnames) VALUES($REMOTE_ADDR."','".$HTTP_USER_AGENT."','".$thedate.".$HTTP_REFERER')");
        mysql_close($link);
    ?>
    That is a simple setup that could easily be improved on but it works.
    You must have the appropriate table with Remote address, User Agent, date, and referrer. I think you can follow what goes on. I just put this in an include file and include it at the top of files I want to log, it needs a place for the current address though.
    I hope that helps some.
    Microsoft's Motto: Resistance is futile, you will be assimilated.

    My dog's name is Jade; she is a Miniature pinscher.
    Click here to see some of her pictures

  3. #3
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats a big help thx

  4. #4
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i was just wondering if you could explain the code (in bold) for me because im pretty new to PHP and i could do with figuring out what this stuff means.


    for ($i = 0; $i < mysql_num_fields ($result); $i++) {
    $meta = mysql_fetch_field($result);
    $colnames["$i"] = $meta->name;

    }
    $thedate = date("F j, Y, g:i a");
    $icolnames = @implode(",", $colnames);
    mysql_query("INSERT INTO $logname ($icolnames) VALUES($REMOTE_ADDR."','".$HTTP_USER_AGENT."','".$thedate.".$HTTP_REFERER')");

    mysql_close($link);

    ?>

    Thanks alot
    Last edited by CHeeSeBLiND; Sep 30, 2001 at 09:00.

  5. #5
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually could you tell me how you display the number of hits aswell. hehe did i mention i was useless?
    hmm...

  6. #6
    SitePoint Member
    Join Date
    Sep 2001
    Location
    michigan
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
        $filename 
    "count.txt";
        
    $counterval 0
        
    $fp fopen($filename,"r"); 
        
    $counterval fread($fp26 ); 
        
    fclose($fp); 
        
    $counterval = (integer)$counterval 1
        
    $fp fopen($filename,"w+");
        if (
    strlen($counterval) == '1')
        {
            
    $temp $counterval;
            
    $counterval '0000'.$temp;
        }
        elseif (
    strlen($counterval) == '2')
        {
            
    $temp $counterval;
            
    $counterval '000'.$temp;
        }
        elseif (
    strlen$counterval) == '3')
        {
            
    $temp $counterval;
            
    $counterval '00'.$temp;
        }
        elseif (
    strlen$counterval) == '4')
        {
            
    $temp $counterval;
            
    $counterval '0'.$temp;
        }
        elseif (
    strlen($counterval) == '5')
        {
            
    $temp $counterval;
            
    $counterval $temp;
        }
            
    fwrite$fp$counterval26); 
        
    fclose$fp ); 

        
    // Uncomment below to just display text. And comment the Graphical part out.
             // echo "$counterval hits"; 

            // below outputs a graphical representation (all images are in /images/num/ )
        // Number images are #_num.gif
        // spc_num.gif is the space between the numbers ( usualy small )
        
    $counterval 0
        
    $num0 fopen($filename,"r"); 
        
    $num fread($num026); 
        
    $Num1 substr ($num01);
        
    $Num2 substr ($num11);
        
    $Num3 substr ($num21);
        
    $Num4 substr ($num3, -1);
        
    $Num5 substr ($num44);

        
    $format "<img src=\"images\\num\\spc_num.gif\"><img src=\"images\\num\\%s_num.gif\"><img src=\"images\\num\\spc_num.gif\"><img src=\"images\\num\\%s_num.gif\"><img src=\"images\\num\\spc_num.gif\"><img src=\"images\\num\\%s_num.gif\"><img src=\"images\\num\\spc_num.gif\"><img src=\"images\\num\\%s_num.gif\"><img src=\"images\\num\\spc_num.gif\"><img src=\"images\\num\\%s_num.gif\">";
        
    printf($format,$Num1,$Num2,$Num3,$Num4,$Num5);
        
    fclose$num0 ); 

    ?>
    Thats my counter, it was the first script i ever made so its a very bulky code. Someone will probably post a reply with the same code just shortend.

    That will display a 5 digit counter in either Text or w/ Images.

    just put this on any page where you want the counter:

    PHP Code:
    <? include("counter.php"); ?>
    -spoon
    CEO nexdot.net
    -spoonified.com


Bookmarks

Posting Permissions

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