Instead of $userId and
session_start();
session_register("userId");
Try $_SESSION['userId'] and
session_start();
You won't need to say global $_SESSION in the login function.
Then to test to see if it's there or not,
if(empty($_SESSION['userId']))
If the version of PHP you're using is older than 4.1 (echo phpversion() to see if you don't know), then you'll want to use $HTTP_SESSION_VARS instead of $_SESSION and use global $HTTP_SESSION_VARS in the login() function.
You don't need the second "global $userId" because you're not inside a function.
And one last point: because your call to login() is placed inside the block controlled by the $userId=="" test, it will only run when $userId=="".