View Full Version : Session Page Views
Hi
Is there a variable I can use in the templates to see the number of page views for a user's current session?
For example, user goes to the site and I want a banner at the top to display some promotion. After they have viewed a few pages I want it to go away and then would only return if they closed the browser and came back to start a new session.
Thanks
I don't know of such variable, but should be rather easy to make. I guess you can use the global hook and just make a new $_SESSION['pagecount'] variable and increment it on every page load.
I think that will work but I can't get it to increment:
$PageCount++;
vB_Template::preRegister('header',array('PageCount ' => $PageCount));
The output is always 1.
tbworld
07-06-2014, 04:07 PM
Not that I know what you are doing, nor would I probably do it this way.
But, if you were going to use @Dave's suggestion, you would have to store it in the superglobal for it to work.
So maybe something like this:
session_start();
$PageCnt = $_SESSION['pagecnt']++;
This will only work for your active session only.
I tried this in Global Start:
$PageCount = $_SESSION['PageCount']++;
vB_Template::preRegister('header',array('PageCount ' => $PageCount));
Then in the Header Template I have this:
Test
{vb:raw PageCount}
{vb:raw $PageCount}
But I go not get any output, perhaps I need a different hook? I tried Global Complete also.
Thanks
tbworld
07-06-2014, 04:44 PM
I am not sure it is the right hook, I would have to look at it, but it should output.
session_start();
$PageCount = $_SESSION['PageCount']++;
vB_Template::PreRegister('header',array('PageCount ' => $PageCount));
Make sure you are looking at the correct style.
For testing purposes make sure it is not nested into a conditional, thus blocking output.
{vb:raw PageCount}
I did it like this:
Test
{vb:raw PageCount}
Test
And do get Test output but not the number. I changed the code to what you put exactly but maybe it should be in someplace else.
There is only 1 style.
Thanks
tbworld
07-06-2014, 05:30 PM
I tried the code on vb4.2.1, again, not that I would actually do it this way, but it worked the way I expected.
Change the hook to "parse_templates" for the most accurate count -- the count will still be off as I guessed.
To test: set "PageCount = 'Test of Page Count'; (Some long string, not a number)
Here is my test code:
// Hook: parse_templates
session_start();
$PageCount = $_SESSION['PageCount']++;
vB_Template::PreRegister('header',array('PageCount ' => $PageCount));
Out of suggestions for right now.
Thanks, that hook worked.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.