HomePHP A Login System for a PHP Email Application
A Login System for a PHP Email Application
We know from the previous article that the user ID is very important, in that it is used to retrieve various information from the database at various stages of the application. The login form sets this userID when you log in. It is the login system that will be the focus of this second part in a four-part series.
For anyone to use the application, they have to be authenticated (be a registered user of the system) or be given an opportunity to become a member. This is what the login system primarily is for. Below is a screen shot of the login page.
The code: connecting and logging in
The login script presents a form in which the user must enter his/her username and password. It is then processed by the various code bits on the page and the appropriate action is taken. Let's go through the various scripts involved.
Connect.php. This script is included on any page that uses the database. It contains the information that connects the application to the database:
<?php $dbname="mail"; $host="localhost"; $dbh=mysql_connect($host) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db($dbname) or die('I cannot select the database because: ' . mysql_error()); ?>
At each stage of the connection attempt, the code makes provisions for errors. This is in case you try to connect to a non-existing database or to a non-existing host.
Login.php. This script presents a login form to the user. This form takes the username and password of the user. The form is presented in a table, as you can see from the code: