Admin And User Login In PHP And MySQL Database
Last Updated : Mar 11, 2024
In this article we will show you the solution of admin and user login in PHP and MySQL database, in php, there are seven Super global variables. In this tutorial, we are going to use the super global variable named $_SESSION.
$_SESSION: a session is a way to store information to be used on multiple pages.
$_SESSION is commonly used in the user login form to indicate that the user is still logged in or logged out on the page.
We are going to use some session variables in the user login page below.
Session_start(): this is used to start the session
$_SESSION[name]: set up a name with some value.
Session_destroy(): remove or destroy the session from the system.
Now we have to create a MySQL database within localhost.
In this tutorial have created a database login and created a table named ‘test’ that has four columns.
- id
- username
- password
- usertype
here user type is either admin or user.
Step By Step Guide On Admin And User Login In PHP And MySQL Database :-
login.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> Simple admin and user login in php and MySQL database </title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <center> <h1 style = "color : rgb(113, 221, 113) ;"> Welcome to TalkersCode </h1> <h3> Simple admin and user login in php and MySQL database </h3> </center> <form action="#" method="post"> <div> <label> username </label> <input type="text" name="username" required> </div> <div> <label> password </label> <input type="password" name="password" required> </div> <div> <input type="submit" value="login"> </div> </form> </body> </html> <?php $host = "localhost" ; $user = "root" ; $password = "" ; $db = "test" ; session_start(); $data=mysqli_connect($host,$user,$password,$db); if($data===false) { die("connection error"); } if($_SERVER["REQUEST_METHOD"]=="POST") { $username=$_POST["username"]; $password=$_POST["password"]; $sql="select * from login where username='".$username."' AND password='".$password."' "; $result=mysqli_query($data,$sql); $row=mysqli_fetch_array($result); if($row["usertype"]=="user") { $_SESSION["username"]=$username; header("location:user.php"); } else if($row["usertype"]=="admin") { $_SESSION["username"]=$username; header("location:admin.php"); } else { echo "username or password incorrect"; } } ?>
- First, we write <! DOCTYPE html> to mention the version of the HTML file
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Attach an external CSS file using <link> tag
- <h1> tag used to add heading here and also adding the inline CSS here.
- Create a <form> with the action of ‘#’ and with the method post
- Add a heading of the Login Page and create two <input> for username and password with names accordingly
- Create another <input> for submit button with the name login
- Write <?php to write php function and close tag with ?>
- Create variables for $host, $user, $password and $db. Set the values of them
- Start the session with the function session_start().
- Now create a variable named with mysqli_connect() to connect with the MySQL database
- If-else statement is created. Into the if statement if the $data is false then use die() for the connection error.
- Into another if statement using $_SERVER with the REQUEST_METHOD of POST. Set the $username and $password with the $_POST method with the user entered username and password
- Create a variable for $sql to select username and password from the database Login
- $result with the variable mysqli_query() and $row with the mysqli_fetch_array() of the result
- Now create another if-else statement. If the usertype is user then set the header() location to the ‘user.php’
- Else if statement is used if the usertype is admin then set the header() location to the admin.php
- Lastly else statement with an echo that username and password is incorrect
User.php
<?php session_start(); if(!isset($_SESSION["username"])) { header("location:login.php"); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> user page </title> </head> <body> <h1> this is the user page </h1> <?php echo $_SESSION["username"] ?> <a href="logout.php">Logout</a> </body> </html>
- Write <?php to write php function and close tag with ?>
- Start the session with the function session_start().
- If the username is not isset then set the header to the login.php
- Then create <html> tag to write html code within it
- <h1> to write a heading of the user homepage
- Using echo to $_SESSION the username
- <a> with the href of logout.php.
Admin.php
<?php session_start(); if(!isset($_SESSION["username"])) { header("location:login.php"); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> admin page </title> </head> <body> <h1> this is the admin page </h1> <?php echo $_SESSION["username"] ?> <a href="logout.php">Logout</a> </body> </html>
- Write <?php to write php function and close tag with ?>
- Start the session with the function session_start().
- If the username is not isset then set the header to the login.php
- Then create <html> tag to write HTML code within it
- <h1> write a heading for the admin homepage
- Using echo to $_SESSION the username
- <a> with the href of logout.php.
Logout.php
<?php session_start(); session_destroy(); header("location:login.php"); ?>
- Write <?php to write php function and close tag with ?>
- At first start the session with session_start() function.
- If statement with the function session_destroy() to destroy the session and lead it to the login page.
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know Simple admin and user login in php and MySQL database.
I hope this article on admin and user login in PHP and MySQL database helps you and the steps and method mentioned above are easy to follow and implement.