How can I make my PHP extension programmed in C++ remember global variables? -
i using php-cpp library develop php extensions.
when try following in c++:
#include <phpcpp.h> static int number=0; php::value get_num() { number++; return number; }
and following in php:
<?php echo get_num(); ?>
everything works expected awhile, "number" variable randomly resets zero. also, pressing ctrl+f5 in firefox, "number" variable again resets zero.
how avoid "number" resetting?
a global c++ variable in php extension not persistent.
it depends on setup of webserver. if use apache example (and others have similar setup), there multiple instances of webserver process running, serving pageviews. each of these instances has own global 'number' variable. that's why not see number incrementing had expected: not every pageview served same apache instance.
on top of that: when load of webserver goes or goes down, new apache processes started , stopped, , new 'number' variables created initial value of 0. also, apache process restarts after fixed number of pageviews (set in apache configuration file), sets counter zero.
in own small testing environment, not run problem fast, because load low can handled single instance of webserver, on live busy server encounter this.
if want use persistent counter, have store somewhere else, example in file or database.
Comments
Post a Comment