Simple Login Page In PHP With Database Source Code
Last Updated : Mar 11, 2024
In this article we will show you the solution of simple login page in PHP with database source code, the purpose of this topic is to learn how to create a PHP MySQL Login System with PHP and a MySQL database.
In this article, you will find a few steps that you need to follow in order to create a login system using MySQL.
The prerequisites for creating the login module need to be discussed before we can create the login system.
We will now discuss the idea of how to login page simply in php with database source code with example.
Step By Step Guide On Simple Login Page In PHP With Database Source Code :-
Config.php
<?php define('DB_SERVER', 'localhost:3036'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', 'rootpassword'); define('DB_DATABASE', 'database'); $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); ?>
Login.php
<?php include("config.php"); session_start(); if($_SERVER["REQUEST_METHOD"] == "POST") { // username and password sent from form $myusername = mysqli_real_escape_string($db,$_POST['username']); $mypassword = mysqli_real_escape_string($db,$_POST['password']); $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'"; $result = mysqli_query($db,$sql); $row = mysqli_fetch_array($result,MYSQLI_ASSOC); $active = $row['active']; $count = mysqli_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count == 1) { session_register("myusername"); $_SESSION['login_user'] = $myusername; header("location: welcome.php"); }else { $error = "Check Your Login Username or Password is invalid"; } } ?> <html> <head> <title>Login Page</title> <style type = "text/css"> body { font-family:Arial, Helvetica, sans-serif; font-size:14px; } label { font-weight:bold; width:100px; font-size:14px; } .box { border:#666666 solid 1px; } </style> </head> <body bgcolor = "#FFFFFF"> <div align = "center"> <div style = "width:300px; border: solid 1px #333333; " align = "left"> <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div> <div style = "margin:30px"> <form action = "" method = "post"> <label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br /> <label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br /> <input type = "submit" value = " Submit "/><br /> </form> <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div> </div> </div> </div> </body> </html>
Welcome.php
<?php include('session.php'); ?> <html"> <head> <title>Welcome </title> </head> <body> <h1>Welcome <?php echo $login_session; ?></h1> <h2><a href = "logout.php">Sign Out</a></h2> </body> </html> Logout.php <?php session_start(); if(session_destroy()) { header("Location: login.php"); } ?>
Session.php
<?php include('config.php'); session_start(); $user_check = $_SESSION['login_user']; $ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' "); $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); $login_session = $row['username']; if(!isset($_SESSION['login_user'])){ header("location:login.php"); die(); } ?>
- A connection between the database and the PHP script is established in the first line of the configuration file.
- An existing session can be resumed or a new one initiated with the session_start() function.
- A POST request is confirmed if the if block checks if it's POST.
- SQL injection is prevented by escaping any special characters in the username and password variables that are returned by the mysqli_real_escape_string() function.
- By using the username and password entered in the admin table, a SQL query searches for the id field within the admin table.
- SQL queries are executed using the mysqli_query() function, and the results are stored in the $result variable.
- Associative array data is received as an input to mysqli_fetch_array() and stored in the $row variable using the mysqli_fetch_array() function. In MySQL, the MYSQLI_ASSOC parameter specifies the association between elements in the array.
- A $row array key named 'active' is assigned to the $active variable.
- By calling mysqli_num_rows(), you count the number of rows returned by the SQL query and record them in a variable called $count.
- A valid login credentials are determined when $count equals 1, resulting in the user being directed to welcome.php using the header() function. Using the session_register() function, the user name is also registered with the session, and is stored in the $_SESSION superglobal array.
- Whenever $count doesn't equal 1, an error message indicates that the login credentials are invalid.
- A login form is created by using HTML code that contains two text boxes: one for username and one for password.
- In this case, the action attribute is set to an empty string and the form is submitted to the same page.
- Using a font size of 11px and a color of #cc0000, the error message is displayed within a div element. Whenever the login credentials are incorrect, the $error variable is populated.
Conclusion :-
As a result, we have successfully learned how to login page simply in php with database source code.
The purpose of this tutorial was to show you how to create a simple login page using PHP and MySQL.
By initializing the $_SESSION variable when the user traverses to other pages and destroying it when he or she logs out, you can detect the user's intent.
I hope this article on simple login page in PHP with database source code helps you and the steps and method mentioned above are easy to follow and implement.