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?