TransWikia.com

how do i prevent logged in users from accessing the login page?

Stack Overflow Asked on January 3, 2022

i have been trying to prevent user that are logged in not to go back to the login page.
please i need your help.
i’ve tried different method, but yet to no avail. i will be much grateful if anyone can help me. Thanks.

here is my php code for login

<?php 
require 'connection.php';
session_start();
    
    $_SESSION['message'] = '';
    
    
    if(isset($_POST['login']))
    {
       if(empty($_POST['student']) || empty($_POST['pass']))
       {
            $_SESSION['message'] = " student id and password is required";
       }
       else
       {
           $password = md5($_POST['pass']);
           $student = $_POST['student'];
            $query= "select studentid, password, status from student_register where studentid='$student' and password='$password'";
            $result=mysqli_query($conn, $query);

            $row = mysqli_fetch_assoc($result);
            if($row)
            {
                $_SESSION['user']=$_POST['student'];
                $_SESSION['stat'] = $row['status'];
                $_SESSION['message'] =" Login successfully";
                header("refresh:5;url= Welcome.php");
            }
            else
            {
                $_SESSION['message'] =" Student id or password is incorrect";
            }
       }
    }
    else
    {
        
    }

?>

One Answer

You can add a small code into you login.php file to check the user already login or not.

if (isset($_SESSION['user'])) 
{
  header("LOCATION: Welcome.php");
}

If session is already set, redirect it back to welcome.php.

Use this code for those pages which require login to access the page.

if (!isset($_SESSION['user'])) 
{
  header("LOCATION: login.php");
}

Answered by mufazmi on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP