1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Top viewed pages

Discussion in 'Traffic Analysis' started by star2323, Mar 2, 2006.

  1. #1
    I want to show a list of the top viewed pages on my site. How do I go about doing this?

    I use PHP and MySQL on the site already. So how do I add code in order to keep track of the number hits each page gets?

    Do I need to do something with cookies, tracking the ip, etc....

    Thanks
     
    star2323, Mar 2, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    At the top of each page you need to add a hit to the database. The best way to do this is to set up a database as follows:

    id, page, number_of_hits
    1,index.php,12

    At the top of the page you select the number of hits for that particular page and then increment this number by 1. Then you write the new number back to the database.

    So when a hit is registered in index.php3 the number 12 is selected and increased to 13 before the database is updated to show 13 hits.

    You should also use sessions to make sure duplicate hits and page refreshes are not counted. You would create a session for each page when the hit was counted and do a check when the page was loaded to see if the session was registered.

    I can provide a script if you need it.
     
    mad4, Mar 2, 2006 IP
  3. star2323

    star2323 Peon

    Messages:
    445
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply.

    The session part is important. I don't want somebody to simply hit refresh to jack up the hit count!

    If you have a basic script I can look at that would be great.

    You can PM me if you like.

    Thanks again.
     
    star2323, Mar 2, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this:
    $page=$PHP_SELF;
    $result = MYSQL_QUERY("SELECT hits FROM hitsdb WHERE page='$page'");
    if (!$row=mysql_fetch_array($result))
    exit();
    				
    if(!session_is_registered("$page")){
    $newhits=1+$row[hits];
    session_register($page);
    $result = MYSQL_QUERY("UPDATE hitsdb SET hits='$newhits' WHERE page='$page'");
    }
    PHP:
     
    mad4, Mar 2, 2006 IP
  5. star2323

    star2323 Peon

    Messages:
    445
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks I'll try it out later!
     
    star2323, Mar 2, 2006 IP
    mad4 likes this.
  6. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You may wind up with "threading issues" if this this is a busy site and two people try to load the same page at the same time. Say person A opens the page when the hit count is 12. Person B opens it at the same time and also gets a count of 12. Then both update the database. Now the hit count is 13 instead of 14.

    To get around this, you can do something like:

    if(!session_is_registered("$page")) {
    session_register($page);
    $result = MySqlQuery("update hitsdb set hits=hits+1");
    }

    Totally untested, but that's the basic idea.

    Good luck. This is an interesting idea.
     
    jimrthy, Mar 2, 2006 IP
    mad4 likes this.