WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[next]

User Personalization with PHP:
User Login

By

Social Bookmark

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume


In this article, we will be looking at the login page of the application. This is the first page that any user wanting to use our application is going to be faced with and the most important one in this section. This script does the very important job of authenticating a user and can make or break the application in the sense that if it is weak security wise, then any attacker can easily penetrate our application and cause damage. In this case, we will try to make it as difficult as possible for attackers to break our application. Some of the things that we are going to do to strengthen our application is to enforce data validation and make sure that we also put some measures in place to stop SQL injection.

The Login Script

The login script plays a very important role in this application. It is responsible for authenticating and keeping track of the user. Without this page any user will simply be able to store any bookmark and there will be no order, in the sense that when a user wants to view her bookmarks she will see everybody else's bookmarks at the same time because she will not have her bookmarks grouped under her name. In addition to authenticating users, the login script also starts a session for each user that it authenticates successfully, to keep track of this particular user. It creates a number of session variables that will eventually be used by other scripts. The script also runs a verification code that keeps automated robot logins at bay. Below is a screenshot of the script followed by the code:

See Figure 1

The code for the script is very large and contains a mixture of PHP and HTML, so I will list it section by section:

The code above is perhaps the most important on the page since it is responsible for verifying a user's credentials and interacting with the database, where the user details are stored. Let's take a look at the code. The very first thing the script does is to include the connect.php script. This script contains the database connection details that we need to connect to a MYSQL database; it also starts a session for the user:

We check if the form has been submitted and set some variables:

//check if the form has been submitted
if(isset($_POST['submit'])){
$msg="";

The $msg variable is used to record any error messages that we may encounter, and is used throughout this script. As an improvement, try to use a form variable to check if the form has been submitted instead of the submit button as I did here. By using a form variable the user has more choice, in the sense that the form will be submitted whether they press the RETURN button on the keyboard or click on the submit button of the form. Once the form has been submitted all the posted information will be available for us to use, but it will also be available for attackers to use. Therefore, we need to do some data validation to make it as difficult as possible for attackers to break our application. Since we require all the fields to be filled in, we need to do three things:

We know that the user needs to submit the username, password and a verification code. Therefore, we check if the form variable is empty and if so, we set an error message:

Then we check to see if the form variable(s) are of the right length:

Note that for security reasons we set a vague error message such as "Invalid password" instead of something more informative such as "Incorrect password length". This is simply to confuse any attacker.

Once we've validated the form variables, we use our flag variable $msg to check if any errors occurred during form validation. If not, we continue to check if the user submitted password and username exists in our database, and if the account is active. We start by checking if the number code entered by the user is the same as the one shown on the form:

If the number codes match, the code connects to the database to verify the user:

If the user checks out, we need to set the session variables. These include:

Here's how the above information is collected:

If the user details did not match, the following error is displayed:

If the number codes do no match then the following error message is shown:


The HTML Form

The HTML page presents the user with a form that collects the following fields:

The page also provides the user with links to the registration and password scripts. It has the following code:

There are two things worth explaining regarding this page; first is that the form is 'sticky.' In other words, whatever the user types in will remain, even if the page is refreshed, the following code ensures this:

The PHP code inside the HTML form elements retrieves the password and username that the user entered previously. Secondly, the form also has the capability to show error messages:

The code above is also included in the form and prints out all error messages that the script encounters while validating the user.


[next]

Recent Articles

WebReference.com site name
Building a Banking Application Home Page with OOP
Mixing Scripting Languages
Review: phpFox, a Social Networking CMS with all the Bells and Whistles
internet.com site name
Enterprise 2.0: Social Networking in the Cloud
BroadSoft Marketplace Hastens Pace of Telephony Innovation
Review: HTC Hero for Sprint


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs