Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment a counter each page load (in PHP)?

I want a certain action to happen when a user has visited X pages of a site

Do I have to store the counter externally (in a txt file or db)?

I can't think of a way to set the counter to 0, then increment it each page load. The counter would always get reset to 0, or am I missing something obvious?

like image 768
meleyal Avatar asked Jan 30 '26 01:01

meleyal


1 Answers

It would be pretty simple to just use $_SESSION data to store how many pages an individual has viewed.

$_SESSION['pageviews'] = ($_SESSION['pageviews']) ? $_SESSION['pageviews'] + 1 : 1;
like image 87
TJ L Avatar answered Jan 31 '26 14:01

TJ L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!