GreenCore's example are correct and can be used to either keep the user on the page or to force the session to end.
It all comes down to being able to send a request for a logout page to the server so that it can kill the session. However you cannot
rely on any solution successfully ending the session, as they generally use JavaScript, which can be turned off, or the user's connection may die etc....
In terms of putting a timeout on the session, PHP by default will do this anyway, as it uses cookies (be it client or server) to control user's sessions, and these have an expiry date of a specified time after the user's last activity on the site - I believe the default is 24 minutes. It is possible to control the timeout length.
This page and the links on it may help:
http://www.webmasterworld.com/forum88/12812.htm
Hope this helps
Phil