The userID session variable should be set when a user logs in, so this check should work to determine if a user is logged in or not.
If you have the login form in an include file that you can just add this code directly into your include file. It should have the same effect as if you put the code directly on the page.