All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

User And Admin Login With Session PHP

Last Updated : Mar 11, 2024

User And Admin Login With Session PHP

In this article we will show you the solution of user and admin login with session PHP, 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.

Step By Step Guide On User And Admin Login With Session PHP :-

Let us see all the code files used in user and admin login with session in php.


<!DOCTYPE html>
<html lang="en">
    <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 and admin login with session php </title>
        table {
            border: 1px solid black;
            background-color: bisque;
        td {
            border: 0px;
            padding: 10px;
        <h1 style = "color : rgb(113, 221, 113) ;"> Welcome to TalkersCode </h1>
        <h3> user and admin login with session php </h3>
    <form action="welcome.php" method="post">
                <h2> Login Page</h2>
                <td> Username: </td>
                <td><input type="text" name="username"></td>
                <td> Password: </td>
                <td><input type="password" name="password"></td>
                <td> <input type="submit" name="login" value="login"> </td>
  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 ‘welcome.php’ and with the method post
  7. Open a <table> and close it with </table> tag
  8. Add a heading of Login Page and create two <input> for username and password with name accordingly
  9. Create another <input> for submit button with the name login


$username = "admin" ;
$password = "admin" ;
session_start() ;
if(isset($_SESSION['username'])) {
    echo "<h1>Welcome ".$_SESSION['username']."</h1>" ;
    echo "<a href='product.php'>Product</a><br>" ;
    echo "<br> <a href='logout.php'> <input type=button value=logout name=logout> </a>" ;
else {
    if($_POST['username']==$username && $_POST['password']==$password) {
        $_SESSION['username'] = $username ;
        echo "<script> location.href='welcome.php'</script>" ;
    else {
        echo "<script> alert (username or password incorrect!') </script>" ;
  1. Write <?php to write php function and close tag with ?>
  2. Create variables for $username and $password. Set the values of it
  3. Start the session with the function session_start().
  4. Now create an if statement for those who already logged in. Within it, if the isset of $_SESSION username is valid, then using <a> tag to hyperlink the page to the ‘product.php’ page.
  5. And also echo to add a button using <a> tag to hyperlink it to the ‘logout.php’ page.
  6. Now create else statement to user who wants to log in. create another if else statement within it. Now within the if statement use the $_SESSION variable with the username and using echo with <script> tag to set the location to welcome.php
  7. Or else if the username or password is incorrect then show a alert using <script> tag


session_start() ;
if(isset($_SESSION['username'])) {
    echo "<h2> Welcome to the product page </h2>" ;
    echo "<br> <a href = 'welcome.php'> <input type=buttom name=back value=back> </a>" ;
else {
    echo "<script> location.href= 'login.php' </script>" ;
  1. Write <?php to write php function and close tag with ?>
  2. Start the session with the session_start() function.
  3. Create an if-else statement of the username’s isset of $_SESSION.
  4. Adding heading of the product page using <h2> and <a> to hyperlink it through ‘welcome.php’
  5. Echo with the <script> tag location of the login.php


session_start() ;
if(isset($_SESSION['username'])) {
    session_destroy() ;
    echo "<script> location.href='login.php' </script>" ;
else {
    echo "<script> location.href='login.php' </script>";
  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 how to user and admin login with session using php.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪