Make a Login System for Your Website: PHP and MySQL

Use These Tips to Create a Website Login

Login system in website

This is a simple website login system that has been very reliable and secure for me. I use it myself on my websites, when a simple system is needed. If you know a little MySQL and PHP, it is very easy to use. Here's how to create a website login system using PHP and MySQL.

First, you will need to have MySQL installed on your computer. I have XAMMP installed and I use PHPmyADMIN for accessing my databases. They are free for download; just search for them online. You also need to create a database in your MySQL. And as you proceed, remember that linux server file names are always case sensitive. LOGIN.PHP is not equal to login.php like it would be on WINDOWS servers.

  1. Run this SQL command in your MySQL database (you must have one):

    CREATE TABLE `users` (
    `id` int(3) NOT NULL auto_increment,
    `login` varchar(8) default NULL,
    `password` varchar(8) default NULL,
    PRIMARY KEY (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;

    This will create the table that will record the usernames and passwords.

  2. Then, create the LOGIN.PHP file. This page will contain the form that will submit the user´s data.

    <?
    session_name
    ("MyLogin");
    session_start();
    session_destroy();

    if($_GET['login'] == "failed") {
    print $_GET['cause'];
    }
    ?>
    <form name="login_form" method="post" action="log.php?action=login">
    Login: <input type="text" name="user"><BR>
    Password: <input type="password" name="pwd"><BR>
    <input type="submit">
    </form>

  3. Now, create the LOG.PHP. This is the file that performs the action of the form.

    <?
    session_name
    ("MyLogin");
    session_start();

    if($_GET['action'] == "login") {
    $conn = mysql_connect("localhost","user","password"); // your MySQL connection data
    $db = mysql_select_db("DATABASENAME"); //put your database name in here
    $name
    = $_POST['user'];
    $q_user = mysql_query("SELECT * FROM USERS WHERE login='$name'");

    if(mysql_num_rows($q_user) == 1) {

    $query
    = mysql_query("SELECT * FROM USERS WHERE login='$name'");
    $data = mysql_fetch_array($query);
    if($_POST['pwd'] == $data['password']) {
    session_register("name");
    header("Location: yourpage.php"); // success page. put the URL you want
    exit;
    } else {
    header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
    exit;
    }
    } else {
    header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
    exit;
    }
    }

    // if the session is not registered
    if(session_is_registered("name") == false) {
    header("Location: login.php");
    }
    ?>

  4. If you paid enough attention, you noticed that the login will lead the user to YOURPAGE.PHP. Add these lines of code to any webpage that you want to secure (including yourpage.php):

    <?
    require("log.php");
    ?>

    Printing the user name in the screen is very easy. Just add this code:

    <? print $_SESSION["name"]; ?>

That´s it. You can now use PHP and MySQL to create a simple website login. It works out fine, but if you have any trouble with it, please contact me!!!

Marcos Riso

WebDeveloper - marcos@chipweb.com.br

 

Share this article!

Follow us!

Find more helpful articles:

Comments

Jul
31

Useful and informative article. One suggestion: use password data type of password field instead of varchar. Then u also need to use passwd() function in reading and writing into this field.

By Syed Feroz Zainvi
Jul
28

Thanks for these tips and also the comments to enhance it.

By Mary Norton
Apr
17

Thank you James! You're right, I corrected my logins ...

By Marcos Riso
Dec
22

One point: You will need a server to run this code, because PHP is a server side scripting language. If you install the XAMMP package, as I wrote in the article, the Apache server will be set automatically in your computer. If you download and install MySQL only, it will not work. You will need to install the Apache Server.

By Marcos Riso
Dec
21

Marcos, good tips. Unfortunately, it all looks like "greek" to me, I think I'll hire a pro!

By Riley Klein