PHP Login System

Starting Out

This article is not meant to assist in the set up the most secure, unbreakable login system that has ever existed. Instead, it’s just meant to give an idea of the types of things that go into a login system and some features you can add to yours. As always, if you are building your own and you need it to be secure, I’d definitely recommend that you do some additional research into security methods, though this article will give you a starting point. Also, to keep the comments section of this organized, if you have a question about a specific section, please post it in the forum. This will keep everything organized and help others get a response to you. Ok, let’s get started!

The first thing we need to do is to set up a database to hold our users' information. Our database will have four simple fields for now. This is just the database to keep track of the username, password, and email. Here is a schematic of the database (though if you make your own, add some creativity to the names):

Database Information
Database Name: my_data
Table Name: userpass
User ID Field: user_ID INTEGER - Auto Increment, Unsigned, Not Null, Key
Username Field: username VARCHAR(20) - Not Null
Password Field: password VARCHAR(35) - Not Null
Email Field: email VARCHAR(50) - Not Null

Safety Tip: When you actually create your own, be creative with these names. Change them up. No one will ever see them except you. Also, it will make it much harder for hackers to map out your database if you make these names individual.

Now we need to create a new database user to have access to the database. For mine, I'll have the following:

Database User Information
DB Username: siteAdmin
DB Password: password

Now we have our database set up and ready to use. Now it is always a good idea to make an include file that will hold data for your entire. This makes movement and change easy. We're going to call it include.php. include.php will contain all the variables and functions that are used by more than one or two files. So here is what we should have in there now. I added a few functions that are always nice to have:

include.php Select All
<?php
/* VARIABLES */
//main site variables
$domainName="http://localhost/login_system/";

//database variables
$dblocation="localhost";
$dbuser="siteAdmin";
$dbpass="password";
$dbname="my_data";
$dbUserTable="userpass";
$dbUserTable_userid="userID";
$dbUserTable_username="username";
$dbUserTable_password="password";
$dbUserTable_email="email";

/* FUNCTIONS */
function strToInt($s)
{
if(!$s)
{
return 0;
}
else
{
$n = ord(substr($s,0,1)) - 48;
return strToInt(substr($s,1)) + ($n*pow(10,strlen($s)-1));
}
}

function encrypt($str)
{
  return md5($str);
}
?>

Now to explain what everything is in this file. The domain name is the path to where you want the "root" directory to be. This is where your site "starts". It will be used to make direct links to anything that needs it. The database variables are from the actual database, so if you used different ones from me, be sure to change them. Also, the freebie function changes a string to an integer. It is a recursive function (it calls itself), so if you want to take a look at it, feel free. There’re easier ways to do this, but this shows an example of a recursive function.

The other function is the encryption method for the users' passwords. Since this will be used in two places (registration and login), adding a function in here will make changing it easier. Feel free to use any encryption method you want, but know that some are better than others. The md5 encryption is a well known scheme with works very well, so I'd suggest sticking with it.

Everything is set up and ready for the actual coding to begin. Time to start coding for the actual site!