简体   繁体   中英

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

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
    {
        
    }

?>

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM