Ivan Venediktov
Ivan Venediktov

Reputation: 158

The most server-friendly way of recording user stats into mySQL with PHP

I am stuck with a situation, where I require to record user's statistics (i.e. record how many pages did the user view). Due to limited resources and high amount of views I can't afford to record stats directly to the database.

My version of the solution was to save stats to the session and then once in every 10 minutes the stats will be recorded to the database. But what happens if user closes the browser in between this 10 minute "checkpoints"? Is there a way PHP script could check the session without user being online?

Upvotes: 1

Views: 113

Answers (2)

Aurel
Aurel

Reputation: 3740

I would use a nosql DB in-memory like Redis for his speed to record stats on each views and then periodically aggregate this data into a mysql table.

Upvotes: 1

Peky
Peky

Reputation: 449

Use Custom session handlers. By implementing SessionHandlerInterface class you can change default behaviour of session destroy and close functions. So you will be able to save your data whenever session is closed or destroyed.

Upvotes: 3

Related Questions