-
Step 1 of 6Hook
Every request is checked
A CodeIgniter hook runs after each controller is built, before any page logic. It skips the command line and admins, and has an on, off and test mode so it could be tried on test accounts first.
-
Step 2 of 6Register
A new sign-in gets a token
A session without a device token registers one: a random token goes into the session and a row with the IP and browser goes into user_devices. Only the last 20 rows per student are kept.
-
Step 3 of 6Edge case
The newest sign-in wins
Registering a device ends every older open row, never newer ones. So when two sign-ins race, the later one always survives, and a student can't be locked out of both devices.
-
Step 4 of 6Enforce
The old device is signed out
On the old device's next request, its row is ended, so the guard clears the session and explains why. Normal pages redirect to the login page; background requests get a 401 JSON reply.
-
Step 5 of 6Edge case
Even in the middle of a video
A small script on student pages calls /my/device-check once a minute, so a lesson left playing on the old device stops too, not just the next time it loads a page.
-
Step 6 of 6Recap
Cheap to run
last_seen is written at most once every 5 minutes to keep database writes low, and signing out ends that device's own row. The schema ships as a dated SQL migration.
Tip: the ← and → keys work too.