Simple PHP/MySQL user login tutorial. Make parts of your website private with a login. |

PHP & MySQL Login Tutorial Series
Pt1: Introduction
Pt2: Setting up the database
Pt3: The front end and user validation
Pt4: The code to privatize a page
Pt5: Logout function and clearing the session ID
Part 1: Introduction
Sometimes it’s important to restrict access to certain parts of a website, and the best way to regulate this access is through a user login system. This article is going to cover the creation of a very simple login system that will help get you started. This system is by no means 100% secure, so we do not recommend using it in a production environment, however we will examine one of the ways a login can be created and we’ll also look at some of the pitfalls of such a system.
Before we begin, let’s establish the pseudocode of our script and walk through the general process of a login. First, and most importantly, we need a user account. This will include the user’s login identification number, a username, password, and any other credentials we may want such as access levels or email addresses. To keep this simple, we’ll stick to user id, username, and password. When a user enters his or her login credentials on your site, we will have to verify the credentials and then establish some means to keep track of a user’s login status. Remember, whenever someone visits a webpage, that page is always loaded as if it’s the first time someone has been on the site. We need a way to tell the site who the user is so that the site can make adjustments for the logged in account.
There are essentially three different parts to this login system. The first part is the PHP code we’re going to write that will regulate the user access. We’ll also need a database to store both the user account information as well as temporary session information that remembers what accounts are logged in. The latter can be setup several ways, including the use of PHP sessions, however for this example we’re going to store the sessions in a database. The third thing we’ll need is a way for the user’s computer to identify itself. For this we’re going to store a cookie on the user’s computer that will be sent to our website every time the user accesses a new page.
Let’s begin creating the code. If you want to test this code on a working site, we suggest you create a separate user.php file (or equivalent) that will store all of the user functions. In this example we’ll simply reference all of the different parts necessary. To view the final working code as it should appear on a site, go to http://blog.readysetconnect.com/sample.php.txt.

(+20 rating, 22 votes)







Leave a Reply