Now we have the base of a login system, so let’s look at a practical usage of these scripts. A page would look like so:
File: example.php
<?php
require 'db_connect.php';
// require our database connection
// which also contains the check_login.php
// script. We have $logged_in for use.
if ($logged_in == 0) {
echo 'Sorry you are not logged in, this area is restricted to registered members. ';
echo '<a href="login.php">Click here</a> to log in.';
exit;
}
// show content
$db_object->disconnect();
// when you are done.
?>
This makes it very easy to restrict access to a document, only a person whose information has been authenticated by check_login.php will be able to view the page, the others will be offered a link to ‘log in.’
More…
There are various ways we can jazz up this little member system, such as a user online script, a member list, member profiles, instant message system… the list goes on and on. This is the bear minumum, it’s up to you to edit it to your needs, if you need any help use the comments system below and someone will answer.
We can use $_SESSION['username'] to interact with the database row associated with the current logged in user, $logged_in to check for a positive login, we can do just about anything now. We could do this:
<?php
require 'db_connect.php';
if ($logged_in == 1) {
echo 'Logged in as '.$_SESSION['username'].', <a href="logout.php">logout</a>';
} else {
echo 'Not logged in. <a href="login.php">Login</a>';
}
?>
Showing the user what name they are logged in as and offering a link to logout, while they are logged in, or telling them they aren’t logged in and offering them a link to do so, if they’re not logged in.
The list really is endless, I cannot really include more, this article is long enough, if you would like to see a how-to on a few things you can do with this, leave a comment below, if there is enough interest I will find the time to write it.