Welcome to the fifth part of a twelve-part series on building a content management system in PEAR. In the last article we looked at some of the application-wide scripts, in other words, scripts that are used by all of the application. In this article we will explore some of the scripts that deal with different sections of the entire application.
User authentication
User authentication in an application such as this is necessary because it provides a way to keep track of who uses the application. It can also be understood as a security measure because you do not want just anyone to use the application and make changes. To make sure that a user is authenticated, we require a password and username. This by implication means that anyone who wants to use this content management system needs to be registered and already be in the database.
The process of authentication works as follows: the user is presented with a HTML form that collects all the required information, which in this case is the username and password. This information is then used to match against information that is held in a database. If the information is also found in the database, the user exists, so we let them to use the CMS; otherwise, the user is returned to the login page. Below is the relevant code:
<?php
ob_start();
session_start();
$err=false;
$error="";
//check if form is submitted
if(isset($_POST['key'])){
//make sure fields are not empty
if(empty($_POST['uname'])){
$err=true;
$error="Please enter a username.<br>";
}
if(empty($_POST['upass'])){
$err=true;
$error .="Please enter a password.<br>";
}
//make sure fields are string
if(is_numeric($_POST['uname'])){
$err=true;
$error .="The username you entered has a invalid format.<br>";
}
if(is_numeric($_POST['upass'])){
$err=true;
$error .="The password that you entered has an invalid format<br>.";