A login script that I wrote which works for me:
Step 1: Authenticate the user
session_start();
if (isset($_POST['username']) {
if($_POST['username'] = $database_username) { // where $registered_username is a username that has been registered and thus is a valid username
$_SESSION['username'] = $_POST['username'];
header("Location: memberpage.php"); // Once authenticated, move the user along to the members-only page
} else {
echo "You did not enter a valid username";
}
} else {
echo "You did not enter a username";
}
I hope that make sense (and since this is the second message I've posted, I also hope that that code is automatically intended, since I don't know how to do it otherwise.
Step 2: Validate the user on other pages.
This is what the members-only pages would look like. I write my page up as usual, but around the entire thing I have:
session_start();
if (isset($_SESSION['username'])) {
// the member-only page goes here
} else {
echo "You are not logged in. To view this page, log in.";
}
So there you go, a person can only view a page if that particular session variable is set, and that session variable can only be set if the user is authenticated by the log in script. A non-member can't simply copy and paste the url or easily fake the session, because there's a session variable there. Also the session variable is useful for when you want to use session_destroy(), because that function gets rid of the session variables, not the session itself (without the session variables set, the visitor can't access member-only pages).
I've never been all that great at explanations, but if you have any questions or if my advice wasn't what you wanted at all, let me know.