Jump to content

Make variable globally available


pwired
 Share

Recommended Posts

Hi,

I have a simple counter on my home page template file, home.php,
that simply counts how many times the home web page is loaded.

$page->homepagehits += 1;
$page->of(false);
$page->save('homepagehits');
$page->of(true);

If I want to echo the value of this counter not only on the home page but on other pages as well,
I know that I can simply do it like this:

On the template file of the other page I put this:

$homepage = $pages->get('/'); 
$counter = $homepage->homepagehits;

and then on this other page I simpy do
<?php echo $counter ?>

Besides doing it this way, my question is, is there another way to make the value of $counter
globally available on all other pages ?

Maybe with an $Data array or something ?

 

Link to comment
Share on other sites

2 hours ago, pwired said:

Besides doing it this way, my question is, is there another way to make the value of $counter
globally available on all other pages ?

Maybe with an $Data array or something ?

Not really. Since you have to store the value, you also need to load the page it is attached whenever you want to read it. PHP provides no built-in persistent storage that reaches across sessions and is shared between individual threads/processes. If you want to avoid the overhead of loading $homepage and the counter for every page view, you could however use a memory cache like memcached or APC, update that when the counter is incremented and read from it when viewing other pages (or fall back to $homepage if its not yet set, e.g. after a server restart).

  • Like 2
Link to comment
Share on other sites

Just tried it, and yes I can confirm, it is working.

Like Kongondo suggested, I have now put this line

$counter = $homepage->homepagehits;

on both the _init.php prepend file and on the home.php template file

Now <php echo $counter ?> shows the correct value on all the other pages as well.

I can use this way of storing values for other cases as well.
Thanks

Link to comment
Share on other sites

maybe is more efficient to use: $config-> homepagehits .. increment it by 1 at home.php template file and later echo it at any template file.. you never need to query any additional page (homepage) to get that counter because $config is present always by default at your hand.. hummm.. is this correct?

Link to comment
Share on other sites

Hi Pixrael,

Quote

you never need to query any additional page (homepage) to get that counter

Thanks for stepping in on this. We´ll, with this line  $counter = $homepage->homepagehits;  on both the _init.php and home.php I now already have the value of $counter globally available. No need to query for it anymore. Great way for storing values also for other cases and make them globally available. Not sure how $config-> homepagehits could be more efficient.

 

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...