AnswerBun.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!

Related Questions

How to solve this UTF-8 encoding C problem?

1  Asked on December 5, 2021 by eamon-ryan

       

What is the @return_value of stored procedure

2  Asked on December 5, 2021 by william-beachy

   

How to create bins in Python

2  Asked on December 5, 2021 by commander

       

Problem in executing code to convert hexadecimal to decimal

2  Asked on December 5, 2021 by user13882705

     

Does this method creates extra space?

1  Asked on December 5, 2021

   

Concatenating two numpy arrays side by side

2  Asked on December 5, 2021 by bare

     

Euclidean Algorithm in Python

2  Asked on December 5, 2021 by luke-newman

       

How do i remove the last 5 characters in a string?

2  Asked on December 5, 2021 by ktulu-hala

     

Basic problems in python

2  Asked on December 5, 2021 by ujjwal

 

comparing with `{}` and truthyness

4  Asked on December 5, 2021 by dcsan

     

List wtih Python

1  Asked on December 5, 2021 by klm

   

Ask a Question

Get help from others!

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