PHP + MySQL Profile View Counter

Posted on 2007-10-06
Last Modified: 2013-12-13
Hi all,

I'm working on adding profile view counters for the following:

I'm having a bit of difficulty figuring out what kind of algorithm to use so that only one counter hit per profile is registered with a user session. I think it makes sense for a users hit to increment the counter on multiple profiles, but only one per session.

Anyway, I'm looking for an elegant solution/algorithm/ideas. I already have a counter field in my table ready to go which defaults to zero for each new registered profile. I'd like to also leave the mathematics to MySQL, versus PHP.

Thank you kindly for your help.

- sf
Question by:stevefNYC
    LVL 21

    Expert Comment

    If I understand you correctly, you are wanting to only increment the counter (by 1) when the user views a profile for the FIRST time (for that session), but preventing the counter from incrementing any more even if the user visits the same profile again within the same session? But the user can view other profiles that they have not viewed in this session before, and it would still increment those corresponding counters?

    If this is what you are looking for, then I would go about it like this: create a array session variable, and store all the unique identifiers of the profiles (profile id??) into the array, one by one as the users view each different profile, this way we can tell what profile the user has already viewed in the particular session.

    Everytime the user views a profile, the code checks to see if the profile id is present in the array, if it is then the user has visited the profile in this session before already, therefore does not increment the counter, otherwise increment the counter by 1.

    A simple function to check whether some value is in the array:

    function searchlist($value,$alist)
                if($alist == '')
                      return false;

                foreach ($alist as $avalue)
                            return true;

                return false;

    This will simply return true if the given value is in the given array ($alist) otherwise returns false if not.

    Let me know how you go.
    LVL 3

    Author Comment

    Well, that sounds like a reasonable solution.

    I have to first see if my PHP framework allows for this type of session registration. I'm not entirely sure I can use an array for values for a particular session variable.

    Is handling a counter like this silly either way?
    LVL 21

    Accepted Solution

    I think it is reasonable to do this, since it gives you a more accurate counter value, and prevents abuse in regards to hitting refresh (F5) multiple times by any user which will then give the counter an inaccurate value.

    Not sure what you mean by your PHP framework allowing session registration, however an array session variable can simply be created and used just as a normal array variable in PHP:

    $somearray[0] = "value1";
    $somearray[1] = "value2";

    $_SESSION["someSessionArray"] = $somearray;

    echo $_SESSION["someSessionArray"][1]; // This should echo value2

    Please let me know how you get on.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now