biber Posted September 30, 2016 Share Posted September 30, 2016 Hi, I'm trying to add a page-counter to the site 'malabu.de' I created a field 'visit_counter' and added it to the template _foot, which is included to all pages. Then I added this to _foot.php (found at processwire.com/talk/topic/12191-make-visit-counter-code-count-once-per-session/): /* 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); } } echo '<br>Besucher: '.$page->visit_counter; The site throws a Fatal error: Exception: Page 1 does not have field visit_counter (in /kunden/183890_24966/webseiten/pw/wire/core/Pages.php line 1292) #0 [internal function]: Pages->___saveField(Object(Page), 'visit_counter', Array) #1 /kunden/183890_24966/webseiten/pw/wire/core/Wire.php(387): call_user_func_array(Array, Array) #2 /kunden/183890_24966/webseiten/pw/wire/core/Wire.php(325): Wire->runHooks('saveField', Array) #3 /kunden/183890_24966/webseiten/pw/wire/core/Page.php(1411): Wire->__call('saveField', Array) #4 /kunden/183890_24966/webseiten/pw/wire/core/Page.php(1411): Pages->saveField(Object(Page), 'visit_counter', Array) #5 /kunden/183890_24966/webseiten/pw/site/templates/_foot.php(35): Page->save('visit_counter') #6 /kunden/183890_24966/webseiten/pw/site/templates/basic-page.php(25): include('/kunden/183890_...') #7 /kunden/183890_24966/webseiten/pw/site/templates/home.php(7): include('/kunden/183890_...') #8 /kunden/183890_24966/webseiten/pw/wire/core/TemplateFile.php(169): require('/kunden/183890_...') #9 [internal functio in/kunden/183890_24966/webseiten/pw/index.php on line 226 I cannot imagine why 'visit_counter' cannot be found. Link to comment Share on other sites More sharing options...
louisstephens Posted September 30, 2016 Share Posted September 30, 2016 Someone correct me if I am wrong, but if I am reading the error correctly, it appears like the field is not attached to the current template (or not created at all). You would need to create the visit_counter field and add to your template. 2 Link to comment Share on other sites More sharing options...
titanium Posted September 30, 2016 Share Posted September 30, 2016 The field is added to the template called _foot, which is included to the page's template. You have to add the field to the page's template itself (to check, try "echo $page->template->name;" - this reveals the "real" page template. 2 Link to comment Share on other sites More sharing options...
pmarki Posted September 30, 2016 Share Posted September 30, 2016 I thought about it as well. If you created a foot template and a foot.php file is included in a _main.php it does't mean that fields from this template will be accessible. You should add visit_counter to page-currently-being-rendered template. Btw.: I use something like this for the same purpose: $page->save('visitCounter', array('quiet' => true, 'uncacheAll' => false, 'resetTrackChanges' => false)); Otherwise on all pages the modified user will be quest. 2 Link to comment Share on other sites More sharing options...
biber Posted September 30, 2016 Author Share Posted September 30, 2016 Hi thanks for Your fast replys. Now I removed the field from the '_foot'-template and added it to the main templates and the error does not appear anymore. But now the counter increases with every page call. I want to count the sessions independently the called pages. I think, this is, what you, @pmarki tries to tell me. But where do I put this line? Link to comment Share on other sites More sharing options...
pmarki Posted September 30, 2016 Share Posted September 30, 2016 I have it in a site/ready.php (if you don't have this file just create one) function bot_detected() { if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) { return TRUE; } else { return FALSE; } } if($session->get('visit_counter_flag') === 0 && !$user->isSuperuser()) { if (bot_detected()) return; /* 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); /* save the visitor_counter field */ $page->set('visitCounter', ($page->visitCounter ?? 0) + 1); $page->save('visitCounter', array('quiet' => true, 'uncacheAll' => false, 'resetTrackChanges' => false)); /* 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); } As a extra bonus this script exludes bots from statistics. 4 Link to comment Share on other sites More sharing options...
Juergen Posted October 1, 2016 Share Posted October 1, 2016 21 hours ago, pmarki said: $page->set('visitCounter', ($page->visitCounter ?? 0) + 1); This gives me an error: unknown ?? Link to comment Share on other sites More sharing options...
adrian Posted October 1, 2016 Share Posted October 1, 2016 28 minutes ago, Juergen said: This gives me an error: unknown ?? The ?? is new to php 7 http://lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator 2 Link to comment Share on other sites More sharing options...
Juergen Posted October 1, 2016 Share Posted October 1, 2016 Thanks adrian. So for lower PHP versions you have to write: $page->set('siteviews', ($page->siteviews)? 0: + 1); Is this correct? Link to comment Share on other sites More sharing options...
netcarver Posted October 1, 2016 Share Posted October 1, 2016 // This might work... $page->set('visitCounter', ((int) $page->visitCounter) + 1); // If not, try this... $page->set('visitCounter', (($page->visitCounter) ? ($page->visitCounter) :0) +1); // Personally, I don't like this style of code - I see too many smileys in there. 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 1, 2016 Share Posted October 1, 2016 Or without all those parenthesis: $page->set('siteviews', $page->siteviews ? $page->siteviews + 1 : 1); Edit: Scratch that. null + 1 does work without error notice: $page->set('siteviews', $page->siteviews + 1); 2 Link to comment Share on other sites More sharing options...
biber Posted October 1, 2016 Author Share Posted October 1, 2016 I have added a file site/ready.php and filled it with pmarki's code, modified with LostKobrakai's line. Then I added a field 'siteviews' to all my templates and put this into the '_foot.php': echo '<br>Besucher: '.$session->siteviews; I also tried '$page->siteviews' and ' $pages->siteviews', but the field still remains empty. Is it necessary to include the site/ready.php? And if, how do I do this? I'm still a beginner in PW and PHP. Link to comment Share on other sites More sharing options...
adrian Posted October 1, 2016 Share Posted October 1, 2016 No need to manually include site/ready.php What you want is: $page->siteviews because you are trying to get the value of that field for the current page. $pages->siteviews won't work at all because $pages needs a ->find or ->get to get the page of interest if you're searching for a page other than the current one ($page). Did you notice that @pmarki's code uses "visitCounter" rather than "siteviews" ? 1 Link to comment Share on other sites More sharing options...
biber Posted October 1, 2016 Author Share Posted October 1, 2016 Hi adrian, thanks for your reply. Yes, I changed visitCounter to siteviews in my code. And I also found a 'field_siteviews' in the database, but it contained no lines even if I visited the site as a guest. But still no output ... Link to comment Share on other sites More sharing options...
adrian Posted October 1, 2016 Share Posted October 1, 2016 1 minute ago, biber said: I also found a 'field_siteviews' in the database, but it contained no lines Yes, every file in PW has a db table to match its name prepended with "field". If there are no rows, then that explains why echo $page->siteviews isn't showing anything. Ton convince yourself that your echo is working, I would manually add a number to the siteviews field for a page (via the page edit interface in the admin). Then you need to debug why @LostKobrakai's: $page->set('siteviews', $page->siteviews + 1); is not working. Are you actually using that, or his pre-edit version? 1 Link to comment Share on other sites More sharing options...
biber Posted October 1, 2016 Author Share Posted October 1, 2016 I will try it tomorrow, thanks and good night. Link to comment Share on other sites More sharing options...
pmarki Posted October 1, 2016 Share Posted October 1, 2016 Sometimes most obvious things aren't obvious, so remember to check if it's working as a quest. if($session->get('visit_counter_flag') === 0 && !$user->isSuperuser())... If you are a superuser it does nothing. 1 Link to comment Share on other sites More sharing options...
biber Posted October 2, 2016 Author Share Posted October 2, 2016 @pmarki: Thanks for that hint, it could have been happened ... But I used a different browser to visit the page as a guest. I found another reason, why your code did not work for me - you wrote: if($session->get('visit_counter_flag') === 0 && !$user->isSuperuser())... as long as a page is not requested yet, there is no 'visit_counter_flag' with the value '0'. I changed '===' to '==' and the counter works. What I have now is a counter that increases when a guest visits the first page of his session. It could be interesting to 1. merge the counters of the single pages to get the number of all visits. 2. count the visit of the second, third ... page within the session. So I had two values: 1. Number of visitors 2. Number of visits of every page. Because I am still a beginner in PHP, I dont know how to realize it. 1 Link to comment Share on other sites More sharing options...
biber Posted October 2, 2016 Author Share Posted October 2, 2016 @adrian: with the modification of pmarki's code (see above) 22 hours ago, adrian said: @LostKobrakai's: $page->set('siteviews', $page->siteviews + 1); works as expected. Link to comment Share on other sites More sharing options...
pmarki Posted October 3, 2016 Share Posted October 3, 2016 16 hours ago, biber said: What I have now is a counter that increases when a guest visits the first page of his session. It could be interesting to 1. merge the counters of the single pages to get the number of all visits. 2. count the visit of the second, third ... page within the session. To be honest I had the same issue and decided to count visits on other pages but home, because I was more interested in how many people visit a new post in a week than how many visits I have so far. In this case I didn't use visit_counter_flag at all assuming that visitors don't come back to posts. So in this line: if($session->get('visit_counter_flag') === 0 && !$user->isSuperuser())... I only check if not superuser, that's why there was an error with ===, sorry about that. As it was sad here: processwire.com/talk/topic/12191-make-visit-counter-code-count-once-per-session/ there are better ways for statistics, our way is suitable for estimates only. If you would like to count visits per page and on a home page you can increase siteviews if it's not a home page and event if visit_counter_flag is not 0. Link to comment Share on other sites More sharing options...
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