to do this, typically i write an authentication library with a function to check a username and password against the db (flatfile, mysql, whatever) and return true or false. if that succeeds, i call a function to set up a session and session vars (username, sig, useful user data). there is also a function to destroy the session.
there is a function 'require_login()' which i call at the top of every protected page. if it can't find $_SESSION['username'] (or some other test) it will redirect to the login page with header('Location: http://www.example.com/login.php') and exit().
login.php has a form to collect username and password and checks the data against the authentication library, initializing a session if it succeeds.