All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Login Session With Database

Last Updated : Mar 11, 2024

PHP Login Session With Database

In this tutorial we will show you the solution of PHP login session with database and today we will understand login page using session with help of database.

We can also use this to make a project and as a tutor to give project other.

But before doing this you have to yourself that what is the concept and how to use this.

Step By Step Guide On PHP Login Session With Database :-

Here, below we create 5 pages that are index.php, registration.php, login.php, welcome.php and last is style.css. our database name is index whereas table name is rl_form.

Now, let us see the below codes.

// login.php

<?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>

// welcome.php

<?php session_start();
$connect = mysqli_connect("localhost", "root", "", "index") or die("connection failed");
if (empty($_SESSION['unname'])) {
    header('location:index.php');
}
if (!empty($_GET['log'])) {
    session_destroy();
}
$name = $_COOKIE['username'];
$query = "select * from rl_form where user = '$name'";
$result = mysqli_query($connect, $query);
$row = mysqli_fetch_assoc($result);
?>
<!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">
    <style>
        td
        {
            padding: 8px;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="inner">
            <form action="index.php" method="post">
                <div style="width: 100%;">
                    <div class="welcome">
                        Welcome <?php echo $_COOKIE['username'] ?>
                        <img src="./uploadimages/<?php echo $row['image'] ?>" alt="" style="width: 150px; height:35px">
                    </div>
                    <br>
                    <div class="login_submit">
                        <a href="index.php?log=1" id="login">
                            Logout
                        </a>
                    </div>
                </div>
            </form>
            <br><br>
            <div class="registration_options">
                <form action="" method="post" enctype="multipart/form-data">
                    <table border="1px" style="border-collapse:collapse ; background-color:black; color:white;width:80%;margin:auto;font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif">
                        <tr>
                            <td>
                                Your Name is :
                            </td>
                            <td>
                                <?php
                                echo $row['user'];
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Your Email is :
                            </td>
                            <td>
                                <?php
                                echo $row['email'];
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Your Password is :
                            </td>
                            <td>
                                <?php
                                echo $row['pass'];
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Your address is :
                            </td>
                            <td>
                                <?php
                                echo $row['address'];
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Your pincode is :
                            </td>
                            <td>
                                <?php
                                echo $row['pincode'];
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Your City is :
                            </td>
                            <td>
                                <?php
                                echo $row['city'];
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Your Country is :
                            </td>
                            <td>
                                <?php
                                echo $row['country'];
                                ?>
                            </td>
                        </tr>
                    </table>
                    <br><br>
                    <div class="registration_submit">
                        <a href="./edit.php" id="save" style="text-decoration:none ;">
                            Edit
                        </a>
                    </div>
                </form>
            </div>
        </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 see that we create a login page with three fields and with a button. In this we also use jQuery with php and MySQL. JQuery is only for alerts you can also remove it. Header is used to move from one page to another. In login we use a query of session, this is because if we login our from our next page then session gets destroyed on this page.
  6. Now, in next after login, welcome.php opens with show the data or anything else which we want to show. We can create a logout button. If we want to show multiple pages then create logout button on every page. One thing to note here that session must be started on every page. Then about logout we have to give link of that page in which we want to destroy our session. We hope you understand this article properly.
  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 login with session using database.

I hope this tutorial on PHP login session with database helps you and the steps and method mentioned above are easy to follow and implement.