Take the 2-minute tour ×

I'm just a little unsure how I should be doing this for a larger site. Normally I'd have a table for sessions and set a new session ID when a user logs in and check for the session id against the user id in the table as well as the time out.

For tracking user movement, I could enter a new row into the sessions table every time the user visits a page instead of just updating one column, but that isn't so efficient.

I'm just unsure as to the 'industry standard' way of doing this. Using MySQL.


share|improve this question

closed as not constructive by j08691, Kermit, SomeKittens Ux2666, Elias Van Ootegem, RivieraKid Oct 22 '12 at 21:36


What do you need the history for? If you just need it for the current session, you could store it as a $_SESSION variable. If you need it to persist between sessions, you would need to do it in a DB table. –  G-Nugget Oct 22 '12 at 19:40
I am using $_SESSION to store the users' session ID, to check for their login etc. Just tracking what pages users have accessed. –  Prash Oct 22 '12 at 19:41
How long does the data need to last? Just for the current session or longer? –  G-Nugget Oct 22 '12 at 19:45
The data MUST be stored for access at a later date. –  Prash Oct 22 '12 at 19:46
add comment

1 Answer

up vote 0 down vote accepted

There are several methods you can use. There is no industry standard. Google, facebook and other smaller sites use cookies to track user movement across pages. When the session ends, you can then use events like onUnload to trigger queries that will store that info in databases.

But it can be also as simple as having a query at the very top of your pages to store the page that the user is on. You can number every page then store that info by concatenating the page number to the page numebr in the datbase. For example, if one visits page number 72, and you want the values to look something like - 64,4,72:



$user_id = $_SESSION['user_id'];

$page_number = 72; //of course this is variable. I've only used this for example

$db_connection = mysqli_connect('host','user_name','password','database');
mysqli_query($db_connection, UPDATE page_views SET view=concat(view, ','.$page_number) WHERE user_id=$user_id;


Getting the number of pages or the exact page is a matter of using explode() to iterate over the views column contents for a user.

share|improve this answer
Hmm, so I could have something like $_SESSION['visits'] += 'whatever'; (dynamic, obviously). And then store that in the database for the session ID for each login/session? One issue I see is what if a user doesn't log out and just closes the browser? Or should I update the table on each page load? –  Prash Oct 22 '12 at 20:01
It's easy to check whether a session exits. You should do this: $session_id = 'session_id'; $save_path = ini_get('session.save_path'); if (! $save_path) { $save_path = '.'; // if this vlaue is blank, it defaults to the current directory } if (! file_exists($save_path . '/sess_' $session_id)) { unlink($session_id); // or whatever your file is called } PHP deletes session data when it becomes stale during every garbage collection. The php.ini file contains the location where sessions are stored. Hence a test whether the session exists would tell you whether the user is still on the site. –  William The Dev Oct 22 '12 at 20:05
Storing the data when the session is closed makes the process more difficult than it needs to be. Inserting a row when a page is viewed is much easier. Storing the pages visited by a user in a session as a string is far from ideal if you want to be able to process the data in the future. Storing on row per page view would make processing the data easier. –  G-Nugget Oct 22 '12 at 20:09
Yup. True. Just like my example query code in my answer. Much easier to implement. –  William The Dev Oct 22 '12 at 20:11
So, row per page, or row per session and update for each page load? –  Prash Oct 22 '12 at 20:53
show 1 more comment
