PHP isn't really built for that, you can use JavaScript and make AJAX calls to a PHP page updating it with user activity.
An accepted way to do what you're apparently trying (use a dynamic session which logs people out after X seconds of inactivity), is to send such an update on every call to the server. If your site is synchronous (i.e. not a one-page application), you can expect users to navigate to different pages so this code can be in the page header.
If you have a one-page application, or expect users to stay on the same page for a long time (nowadays quite common), you can send an AJAX request every X seconds that updates the server.
WordPress takes both approaches, for example.