SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Page View Counting...

    Hello,

    I'm not quite sure where this should be, so I apologize if this is the wrong section.

    But I have a question about how sites count the number of views a page has got. Many sites have a counting system that displays the number of views a page got, for example this thread, if you were to go back to the topic view page, you can see the number of views it got.

    So I would like to know how people do this, is it a simple int variable that is added to 1, the thing is where would you place this variable?

    I'm sort of new to PHP, so please explain any terms if possible.

    Thanks in advance!

  2. #2
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could write the number of page views out to a file (using fopen() with LOCK_EX to prevent multiple requests from editing the file at once), or you could store the number of page views in a database, such as MySQL or Oracle.

    Good luck.

  3. #3
    SitePoint Member
    Join Date
    May 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, that was what I was thinking, store it into a MySQL database and then call it back whenever I want need it. So this is what I was thinking...

    On the page where I want to count # of views for...
    I initialize a variable and set it to the number of views kept in the database and then increase it by one and then send it back to the db.

    But the thing is, wouldn't this be tedious work when you are working with more than hundreds of pages that you want to count for?

    So surely there is some other easier way to do this method?

  4. #4
    SitePoint Evangelist ikeo's Avatar
    Join Date
    Oct 2004
    Location
    Austin Texas
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Usually the pages, in question, are stored in the Mysql database themselves, but the way you would do it is that at the top of the page you'd put in php to update the database column holding the views for the page. (the row would have unique id relating to the page) and the SQL would simply be.

    UPDATE table
    SET views = views+1;

    Not hard.
    You might want to set up a session so that the page doesn't get updated more than once for a given user.

    Quote Originally Posted by the1 View Post
    Hmm, that was what I was thinking, store it into a MySQL database and then call it back whenever I want need it. So this is what I was thinking...

    On the page where I want to count # of views for...
    I initialize a variable and set it to the number of views kept in the database and then increase it by one and then send it back to the db.

    But the thing is, wouldn't this be tedious work when you are working with more than hundreds of pages that you want to count for?

    So surely there is some other easier way to do this method?


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
  •