Jump to content

make visit counter code count once per session


EyeDentify
 Share

Recommended Posts

Hello Everyone.

I have previous posted a tutorial in the Tutorial section about a counter system for counting visits to a page in PW.

The code works an all is fine and dandy.

Now i am trying something fancy pants... and making the code count a visitor once per session.

This seems however not be the case and the visit is counted again on every page load.

I must have done something wrong somewhere. I maybe have gone a little blind looking at this code.

Am i wrong in testing this code by:

  1. Login to admin
  2. Then visit the page
  3. Check if count goes up ? no it does not so that works.
  4. Log out from Admin then visit page again and reload a few times
  5. Once again log into admin, and yes... all the reloads are counted... damit.

Is my session reset somehow from me logging in and out of admin ?

Any pointers or tips would be helpfull. :)

 

<?PHP

/*
    simple code for recording current page visit count to its visit_counter field of "integer" type.
    But only if the visitor is not currently logged in.
*/

/* set a counter flag to use with the counter */
$session->set('visit_counter_flag', 0);

if($user->isLoggedin()) {
    
    /* if the user is logged in */ 
    
} else {
    
    /* check if the flag is set to 1, if so then do not count the visit */
    
    if($session->get('visit_counter_flag') === 0) {
    
        /* if the user is NOT logged in and not counted */

        /* turn of output formating so PW do not give an error when we change the value */
        $page->of(false);

        /* increment the current integer plus one */
        $page->visit_counter++;

        /* save the visitor_counter field */
        $page->save('visit_counter');

        /* turn on output formating so PW work as it should */
        $page->of(true);
        
        /* set a visit counter flag to 1 so next load do not count */
        $session->set('visit_counter_flag', 1);
    }
    
}
?>

Thank you in advance.

Link to comment
Share on other sites

This is because you are setting the session to 0 every time the page loads, the reason it's working if you are logged in is because of the $user->isLoggedin().

You don't need anything else other than:

    if($session->get('visit_counter_flag') === 0 && !$user->isLoggedin()) {
    
        /* if the user is NOT logged in and not counted */

        /* turn of output formating so PW do not give an error when we change the value */
        $page->of(false);

        /* increment the current integer plus one */
        $page->visit_counter++;

        /* save the visitor_counter field */
        $page->save('visit_counter');

        /* turn on output formating so PW work as it should */
        $page->of(true);
        
        /* set a visit counter flag to 1 so next load do not count */
        $session->set('visit_counter_flag', 1);
    }
  • Like 1
Link to comment
Share on other sites

This is because you are setting the session to 0 every time the page loads, the reason it's working if you are logged in is because of the $user->isLoggedin().

You don't need anything else other than:

    if($session->get('visit_counter_flag') === 0 && !$user->isLoggedin()) {
    
        /* if the user is NOT logged in and not counted */

        /* turn of output formating so PW do not give an error when we change the value */
        $page->of(false);

        /* increment the current integer plus one */
        $page->visit_counter++;

        /* save the visitor_counter field */
        $page->save('visit_counter');

        /* turn on output formating so PW work as it should */
        $page->of(true);
        
        /* set a visit counter flag to 1 so next load do not count */
        $session->set('visit_counter_flag', 1);
    }

Ofcourse :) Your right....

How did i miss that :) ... guess sometimes you go blind.... :)

Thank you pwired. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...