All TalkersCode Topics

Follow TalkersCode On Social Media

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

Admin And User Login In PHP And MySQL Database

Last Updated : Mar 11, 2024

Admin And User Login In PHP And MySQL Database

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";
 }
}
?>
  1. First, we write <! DOCTYPE html> to mention the version of the HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag
  5. <h1> tag used to add heading here and also adding the inline CSS here.
  6. Create a <form> with the action of ‘#’ and with the method post
  7. Add a heading of the Login Page and create two <input> for username and password with names accordingly
  8. Create another <input> for submit button with the name login
  9. Write <?php to write php function and close tag with ?>
  10. Create variables for $host, $user, $password and $db. Set the values of them
  11. Start the session with the function session_start().
  12. Now create a variable named with mysqli_connect() to connect with the MySQL database
  13. If-else statement is created. Into the if statement if the $data is false then use die() for the connection error.
  14. 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
  15. Create a variable for $sql to select username and password from the database Login
  16. $result with the variable mysqli_query() and $row with the mysqli_fetch_array() of the result
  17. Now create another if-else statement. If the usertype is user then set the header() location to the ‘user.php’
  18. Else if statement is used if the usertype is admin then set the header() location to the admin.php
  19. 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>
  1. Write <?php to write php function and close tag with ?>
  2. Start the session with the function session_start().
  3. If the username is not isset then set the header to the login.php
  4. Then create <html> tag to write html code within it
  5. <h1> to write a heading of the user homepage
  6. Using echo to $_SESSION the username
  7. <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>
  1. Write <?php to write php function and close tag with ?>
  2. Start the session with the function session_start().
  3. If the username is not isset then set the header to the login.php
  4. Then create <html> tag to write HTML code within it
  5. <h1> write a heading for the admin homepage
  6. Using echo to $_SESSION the username
  7. <a> with the href of logout.php.

Logout.php

<?php
session_start();
session_destroy();
header("location:login.php");
?>
  1. Write <?php to write php function and close tag with ?>
  2. At first start the session with session_start() function.
  3. 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.

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 🡪