This tutorial will go through the real basics of setting up a PHP login for your website. It makes use of a few simple features of PHP and a mySQL database. This tutorial assumes you have a basic knowledge or HTML, PHP and SQL as well as access to a web server (with PHP & mySQL)

Getting Started

First step is to have your html editor open with a new PHP page. We are going to set up a “config.php” file which will deal with connecting to the database, selecting the appropriate table and checking if the user is logged in or not. This file will then be included onto every page which is to be controlled by the login system, except the login page itself. E.g. (Account Settings, Inbox etc.)

if($_SESSION['LOGGED_IN'] != 1)
    header("Location: login.php");
$servername = "localhost";
$username = "root";
$password = "";
$connection = mysql_connect($servername,$username,$password);
    die ("Could Not Connect" . mysql_error());
mysql_select_db("tablename", $connection); 

Creating the Login Page

A login page is needed for the user to provide the system with their unique username and password. This is done by creating a HTML for to provide the UI for the user to enter their details.

<form action="#" method="post">
 Username:  <input type="text" name="username" /><br />
 Password <input type="password" name="password" />

This small form can be included on a larger page. You may notice that for the action I have left it blank, this is because I want the form to post the data to this page.

At the top of this page we will need to include abit of PHP to check that the username and password is correct, then if so, grant access to the user, redirecting them to the correct page.

if($_SESSION['LOGGED_IN'] == 1)
        header("Location: index.php");
if(isset($_POST['username']) && isset($_POST['password']))
        $username = $_POST['username'];
        $password = $_POST['password'];
        $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
        $result = mysql_query($sql);
        $num_rows = mysql_num_rows($result);
        $record = mysql_fetch_array($result); 
if($num_rows == 1)
        $_SESSION['LOGGED_IN'] = 1;
        $_SESSION['USER_TYPE'] = $record['user_type'];
        header('Location: index.php");

         header('Location: login.php?error=true');


This is reliant on the existance of a mySQL table which contains the username and password for each user. The piece of code above will check to see if they are logged in currently. If they are then it will send them to the home page, if not then it will check to see if the username and password match that which are in the database, check that it is unique and then set sessions to verify the login successful and then redirect to the homepage.

This will drive a simple PHP/mySQL login. Feel free to leave any comments asking any questions. I have purposely left this brief as understanding improves if left to understand the concept alone.


Related Info
One of the important things to consider when buying a new printer is the Printer toner cartridges price, many times the printer will be cheap but its toner cartridges might be very expensive.

If you are into mining, no matter if it is gold, coal, heavy or light mining, when lookin for you should always consider to compare new mining equipment cost to costs.

For a slow PC or to correct most of the errors on your PC, you should try the and get your PC speed back.

These Christmas gift baskets and holiday gift ideas make great presents.

If you own an iPad and you are asking yourself than you just have to read this . Discover the full potential of your iPad.

Online education is also a great invention which comes with the help of information technology and online degree programs.


No comments

Be the first one to leave a comment.


Top 100 Writers – Niche Sites

Here are Top 100 Writers sites that specialize in specific niches:

Weight Loss
Hosting - Available
Computers - Available
Home - Available

More Links