All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Registration And Login Form In PHP And MySQL

Last Updated : Mar 11, 2024

Registration And Login Form In PHP And MySQL

In this article we will show you the solution of registration and login form in PHP and MySQL, are you haunting for the perfect registration and login form using php and sql? Then grab this article helps you with that.

This sample program and guidelines make you create a registration and login forms on your own.

Besides, you can using session hold user details separately to welcome user to their profile section with welcome message and their name.

Here login user information will deleted when the user clicks logout.

Step By Step Guide On Registration And Login Form In PHP And MySQL :-

To execute connection between database and html login and registration forms con.php file created with appropriate block of codes.

The regis.php file created with codes that allows new user to register their information on database for the first time.

It helps to check that user login info when they try to login next time in this platform.

For login page login.php file created with necessary codes and sessionphp.php file checks user name is whether point correct or not then brings them to their dashboard through dasbrd.php file.

When user clicks on logout option in their page their session will destroy by logout.php file and they need to enter by login when they try next time.

con.php

<?php
$con=mysqli_connect("localhost","root","","dbase");
if(mysqli_connect_errno()){
    echo("Failed to connect: ".mysqli_connect_errno());
}
?>

regis.php

<html>
    <head>
        <title>Registration</title>
    </head>
    <body>
        <?php
        require('con.php');
        if(isset($_REQUEST['uname'])){
            $un=stripslashes($_REQUEST['uname']);
            $un=mysqli_real_escape_string($con,$un);
            $ps=stripslashes($_REQUEST['password']);
            $ps=mysqli_real_escape_string($con,$ps);
            $em=stripslashes($_REQUEST['email']);
            $em=mysqli_real_escape_string($con,$em);
            $crt_dt=date("Y-m-d H:i:s");
            $q="INSERT into `userlevels` (uname, email, pass, datetime) VALUES('$un','$em','$ps','$crt_dt')";
            $res=mysqli_query($con, $q);
            if($res){
                echo "<div><h3>You are registered successfully.</h3><br>
                        <p>Click here to <a href='login.php'>Login</a></p></div>";
            }
            else{
                "<div><h3>Required fields missing.</h3><br>
                <p>Click here to <a href='login.php'>registration</a></p></div>";
            }
        }
        else{
        ?>
        <form action="" method="post">
            <h1>Registration</h1>
            <input type="text" name="uname" placeholder="username" required>
            <input type="text" name="email" placeholder="email address" required>
            <input type="password" name="password" placeholder="password" required>
            <input type="submit" name="submit" value="Register">
            <p><a href="login.php">Login</a></p>
        </form>
        <?php
    }
    ?>
    </body>
</html>

login.php

<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
    require('con.php');
    session_start();
    if (isset($_POST['username'])) {
        $username = stripslashes($_REQUEST['username']);
        $username = mysqli_real_escape_string($con, $username);
        $password = stripslashes($_REQUEST['password']);
        $password = mysqli_real_escape_string($con, $password);
        // Check user is exist in the database
        $query = "SELECT * FROM `userlevels` WHERE uname='$username'
                     AND pass='$password'";
        $result = mysqli_query($con, $query) or die(mysql_error());
        $rows = mysqli_num_rows($result);
        if ($rows == 1) {
            $_SESSION['username'] = $username;
            header("Location: dasbrd.php");
        } else {
            echo "<div class='form'>
<h3>Incorrect input.</h3><br/>
<p class='link'>Click here to <a href='login.php'>Login</a> again.</p>
</div>";
        }
    } else {
?>
<form action="" method="post" name="login">
<h1>Login</h1>
<input type="text" name="username" placeholder="Username" autofocus="true"/>
<input type="password" name="password" placeholder="Password"/>
<input type="submit" value="Login" name="submit" />
<p class="link"><a href="regis.php">New Registration</a></p>
</form>
<?php
    }
?>
</body>
</html>

sessionphp.php

<?php
session_start();
if(!isset($_SESSION["username"])){
    header("Location: login.php");
    exit();
}
?>

dasbrd.php

<?php
include("sessionPHP.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard </title>
</head>
<body>
<p>Hiii, <?php echo $_SESSION['username']; ?>!</p>
<p>You are in user dashboard page.</p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
logout.php
<?php
    session_start();
    if(session_destroy()) {
        header("Location: login.php");
    }
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. Using mysqli_connect() line connection requested between server and database ‘dbase’. To create platform for new user register their information regis.php file created.
  4. As foremost step, con.php called once then inside form block created with input fields, ‘name, email, password, submit button’. Here input fields must present with (name, placeholder and type) attributes with respective values.
  5. Using if condition name whether set or not is checked if is set then username, password, email user entered inputs retrieved and stored on variables ($un,$ps,$em). Along with current timestamp was collected and stored on variable $crt_dt through date() method.
  6. Using insert query new users gathered information trying to sent to table ‘userlevels’. With the help of mysqli_query() query executed to store all details to table userlevels thereafter respective response message throws on client side to notify users.
  7. In login.php file form created with two input fields ‘username and password’ to check whether login page entering user is right person or not. If they right person session_start() creates session for that user where username was stored temporarily.
  8. Up next user brings to dashboard page with message Hiii username and You are in user dashboard page.
  9. At last logout.php file created to destroy current user session by line session_destroy().
  10. Each file is important to achieve real time platform alike look and functions, so modify with careful and don’t miss any details.
  11. Echo statement will print message as based on your execution result. Instead echo() you can prefer print() method if you want and that not going to affects your result.

Conclusion :-

In conclusion, hope now you know how to create registration and login form using php and sql.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get any error while running output.

When we execute this program on browser it will display three input fields to register end user needs to enter their details and it throw message ‘You are registered successfully.

And login option’. User have to login with their registered mail and password without error.

It welcome that user with message ‘Hiii username, You are in user dashboard page’ with logout option.

When user wants to logout they just logout from their account by simply clicks on logout option.

I hope this article on registration and login form in PHP and MySQL helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪