February 3, 2024 5:02 am

How to create Login and Signup System in PHP

Received many request from PHP web developer for very basic article to create login and signup page in php. This article is totally for PHP developers who is very new in programming. In this tutorial I have created 2 forms for login and signup with code download and a demo.

How to create Login and Signup form in PHP

[wpdm_file id=44]DEMO

Database Details:

database name => phpgang

table name => users

db.sql

Database file run in your MySQL to create database and add data in table.

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(240) NOT NULL,
  `email` varchar(240) NOT NULL,
  `password` varchar(240) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

db.php

Edit this file as per your database credentials.

<?php
$connection = mysqli_connect('db_host','db_user','db_password','db_name') or die(mysqli_error($connection));
?>

This time I have used mysqli_connect as mysql_connect is deprecated in PHP 5.5 so web developers start shifting your apps to mysqli.

Now come to the main index file in this index file i have done all work like forms and php code to signup and login users.

Note: MD5 is no more secure passwords so please use this class for passwords: How to Hashing Password in PHP 5.5 with Password Hashing API

<?php
include('db.php');
if(isset($_POST['action']))
{          
    if($_POST['action']=="login")
    {
        $email = mysqli_real_escape_string($connection,$_POST['email']);
        $password = mysqli_real_escape_string($connection,$_POST['password']);
        $strSQL = mysqli_query($connection,"select name from users where email='".$email."' and password='".md5($password)."'");
        $Results = mysqli_fetch_array($strSQL);
        if(count($Results)>=1)
        {
            $message = $Results['name']." Login Sucessfully!!";
        }
        else
        {
            $message = "Invalid email or password!!";
        }        
    }
    elseif($_POST['action']=="signup")
    {
        $name       = mysqli_real_escape_string($connection,$_POST['name']);
        $email      = mysqli_real_escape_string($connection,$_POST['email']);
        $password   = mysqli_real_escape_string($connection,$_POST['password']);
        $query = "SELECT email FROM users where email='".$email."'";
        $result = mysqli_query($connection,$query);
        $numResults = mysqli_num_rows($result);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // Validate email address
        {
            $message =  "Invalid email address please type a valid email!!";
        }
        elseif($numResults>=1)
        {
            $message = $email." Email already exist!!";
        }
        else
        {
            mysql_query("insert into users(name,email,password) values('".$name."','".$email."','".md5($password)."')");
            $message = "Signup Sucessfully!!";
        }
    }
}

?>
<!-- Login and Signup forms -->
<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Login</a></li>
    <li><a href="#tabs-2" class="active">Signup</a></li>

  </ul>                 
  <div id="tabs-1">
  <form action="" method="post">
    <p><input id="email" name="email" type="text" placeholder="Email"></p>
    <p><input id="password" name="password" type="password" placeholder="Password">
    <input name="action" type="hidden" value="login" /></p>
    <p><input type="submit" value="Login" /></p>
  </form>
  </div>
  <div id="tabs-2">
    <form action="" method="post">
    <p><input id="name" name="name" type="text" placeholder="Name"></p>
    <p><input id="email" name="email" type="text" placeholder="Email"></p>
    <p><input id="password" name="password" type="password" placeholder="Password">
    <input name="action" type="hidden" value="signup" /></p>
    <p><input type="submit" value="Signup" /></p>
  </form>
  </div>
</div>

In signup process we are checking email validation (!filter_var($email, FILTER_VALIDATE_EMAIL)) and duplicate email no more than one account with single email and encrypt your password with md5().

In demo design i have used jQuery UI tabs and create some css effect on input boxes.

CSS to show input boxes effects:

<style type="text/css">
input[type=text]
{
  border: 1px solid #ccc;
  border-radius: 3px;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
  width:200px;
  min-height: 28px;
  padding: 4px 20px 4px 8px;
  font-size: 12px;
  -moz-transition: all .2s linear;
  -webkit-transition: all .2s linear;
  transition: all .2s linear;
}
input[type=text]:focus
{
  width: 400px;
  border-color: #51a7e8;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.1),0 0 5px rgba(81,167,232,0.5);
  outline: none;
}
</script>

If you have any problem regarding this tutorial configuration please feel free to comment we love to answer your queries.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

57 responses to “How to create Login and Signup System in PHP”

  1. siddhu says:

    nice but little bit lengthy..

  2. Anthony says:

    can you show password recovery(forgot password) function ? via email i think.
    thanks in advance !

  3. Anthony says:

    forgot password function will ba very helpful ! Regards !

  4. Anthony says:

    “Remember me” checkbox will be great too !

  5. bob says:

    Agree with Anthony -(remember me) function will be awesome!

  6. Shahbaz Ahmed Bhatti says:

    Why we useWhy we USe this mysqli_real_escape_string

    What is Purpose o fthese Function

  7. asz says:

    i couldnt download the code although already subscribed

  8. gani says:

    not able to insert record in database using sign up form.

  9. zppinto says:

    Some mistakes I’ve found and respective corrections…

    1. Missing semicolon after CURRENT_TIMESTAMP.
    2. Change line 38 to mysqli and don’t forget to add the $connection parameter.
    3. Add echo $message on line 41 to see the errors.

  10. techviha.com says:

    nice article, i have done this. 🙂 ty for sharing

  11. ARUN DANEGOUDAR says:

    I’m working with bootstrap framework …
    $message not working….

  12. Alndoah says:

    Where can I find the member’s area after login tutorial?

  13. ravi says:

    i am getting error .
    i am using mysql database , should i use mysql instead of mysqli in code of php.

    • Greg Brow says:

      Line 38 change to.
      mysqli_query($connection,”insert into users(name,email,password) values(‘”.$name.”‘,'”.$email.”‘,'”.md5($password).”‘)”);

  14. ravi says:

    how should i name the database db.sql . and what should be the name of css file

  15. esteban Peralta says:

    mysqli_real_escape_string() expects parameter 1 to be mysqli

    help u.u

  16. rubab says:

    having problem with the layout.css is not working for me

  17. preet says:

    do i have make 1 file or 3 files for this coding. thanks

  18. preet says:

    hi i am creating online we store and i want make signup and login form. i dont know php please can some one write full coding for me please i need to make login and signup form on my web store my database name is “phpgang” and table name is “users”………please some one help me thanks a lot in advance

  19. Gustavo Rocha says:

    Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:wampwwwphpgangindex.php on line 38

  20. Goutham says:

    while login it shows only the message . i need a secure login process for a page where only a members can access the page?

  21. Gustavo Rocha says:

    still: Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:wampwwwphpgangindex.php on line 38

  22. Eloi Vicenç Gonzalez Mas says:

    How can I make a login form that redirects each user to its OWN page so that only that user sees that page?

    Thanks in advice!

  23. Qaisar Khan says:

    Great website great article many thanks 🙂 Sir

  24. Naseha Sameen says:

    New to this php, learning … I tried using this code, but it does not insert any record to the table.

    Can you help me.

    • Huzoor Bux says:

      You must got some error try to debug and send error.

      • Ashish Tomar says:

        do i have to link this index file with any html file to see those forms in google chrome. because when i opened index.php in chrome it showed me source code not the forms to login and signup.rep asap

  25. Prakash says:

    The files in download are correct but the code written above has errors.

    Lot of unclear points…….

    1.What is the name of main php page ?
    2.What is the name of .css file ?
    3.Where to put the css code ?

    Error correction:

    There should be a comma after
    `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

    like
    `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

    There is no “forget password” option in the downloaded code too.

  26. Ashish Tomar says:

    @huzoorbux :disqus do i have to link this index file with any html file to see those forms in google chrome. because when i opened index.php in chrome it showed me source code not the forms to login and signup.rep asap

  27. Billions says:

    please i have downloaded the code, am running apache server but still get this error, Need some help

    Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocslogin-signup-in-phpdb.php on line 2

    Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:xampphtdocslogin-signup-in-phpdb.php on line 2

    Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:xampphtdocslogin-signup-in-phpdb.php on line 2

    thank you for helping.having a project i need to fix

  28. jonthan says:

    #1064 – You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near
    ‘(`id`)
    ) ENGINE=MyISAM’ at line 7 how to fix

  29. Golda-Lee Sawyers says:

    Thanks for the article, I am having an issue with line 98 ‘.$message.’. This is my error:

    Notice: Undefined variable: message in C:wampwwwPUNISHMENTindex.php on line 98

  30. Golda-Lee Sawyers says:

    Thank you for this post, very helpful.

  31. Delawar says:

    using pdo registration + sign up source code????

  32. munawar says:

    what is the purpose of using real escape string function ??

  33. Neha Bethu says:

    where should i include this css script??? plz post the answer immediatly

  34. Alberto Giardino says:

    css Does not work…

  35. aradhya says:

    sir it always shows invalid password.

  36. Albert Pinto says:

    what is the use of $message ?
    I didnt understand why u used it?

Leave a Reply

Your email address will not be published. Required fields are marked *