All TalkersCode Topics

Follow TalkersCode On Social Media

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

Login And Registration Form In PHP Using Session

Last Updated : Mar 11, 2024

Login And Registration Form In PHP Using Session

In this tutorial we will show you the solution of login and registration form in PHP using session, today we are going to understand how to create a login and registration form in php with session.

In previous article from html, we already done that how to create a login and registration form.

But in this tutorial we are going to understand the same thing with session in php. Let us see how to apply sessions on forms in php.

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

Here, mainly as we know that session are used to store information same as cookies but in different way.

Cookies are mainly client side whereas session are server side. Once you destroyed your session, you are not able to get information stored in session.

So, we use database to store data with sessions. Let us code for login and registration using php with session.

<?php session_start();
    $connect=mysqli_connect("localhost","root","","index") or die("Connection Failed");
    if(!empty($_POST['button']))
    {
        $name=$_POST['username'];
        $email=$_POST['email'];
        $pass=$_POST['password'];
        if($email== "" && $pass == "" && $name=="")
        {
            echo "<script> alert('Please fill all fields.......')</script>";
        }
        else
        {
            $query= "select * from rl_form where email='$email' and pass='$pass' and user='$name'";
            $result=mysqli_query($connect, $query);
            $count= mysqli_num_rows($result);
            if($count>0)
            {
                $_SESSION['unname']="set";
                setcookie("username",$name);
                header("location:welcome.php");
                // echo "<script> alert('Congratulations! you are logged in .......')</script>";
            }
            else
            {
                echo "<script> alert('Username/Email and Password does not match.......')</script>";
            }
        }
    }
    if (!empty($_GET['log'])) {
        session_destroy();
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Login Form
            </div>
            <div class="login_options">
                <form action="" method="post">
                    <div class="lleft_body">
                        <label for="">
                            Enter your username here
                        </label>
                        <br>
                        <label for="">
                            Enter your email here
                        </label>
                        <br>
                        <label for="">
                            Enter your password here
                        </label>
                    </div>
                    <div class="lright_body">
                        <input type="text" name="username" id="">
                        <br><br>
                        <input type="email" name="email" id="">
                        <br><br>
                        <input type="password" name="password" id="">
                    </div>
                    <div class="login_submit">
                        <input type="submit" value="Login" id="login" name="button">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as you can see that we create a login page using session. Mainly there is no need to apply sessions on registration page, but if you want to apply session of that you can get reference from the code given above.
  6. Now, we start session here. It is mandatory to start session where you want to apply it. Now, in next case we use some validation on login form, you have no need to do that. At last or say in next page where our log is our button. We use there session destroy. We hope that you understand this article easily.
  7. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to create a login and registration form in php with session.

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