EyeDentify Posted February 11, 2016 Posted February 11, 2016 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: Login to admin Then visit the page Check if count goes up ? no it does not so that works. Log out from Admin then visit page again and reload a few times 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.
Tom. Posted February 11, 2016 Posted February 11, 2016 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); } 1
pwired Posted February 11, 2016 Posted February 11, 2016 https://processwire.com/talk/topic/2868-count-views-of-post/
EyeDentify Posted February 11, 2016 Author Posted February 11, 2016 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now