Home > Mobile >  Redirect Loop happen when the session is not set
Redirect Loop happen when the session is not set

Time:05-14

"Session.php"

if(!isset($_SESSION['username'])) 
        {                                       
           header('Location: ./index.php'); // Redirecting To Home Page per il login
           exit;
        }
session_start();// Start new session or resume existing session
    //echo 'session_id: '.session_id();
    
    require "connessione.php";
    $connessione = connessionedb("db");
    
         
    $username=$_SESSION['username'];
    $username = $connessione->real_escape_string($username);
    
    $query = "SELECT * FROM utenti where idUtente= '$username' ";
    $risultato = mysqli_query($connessione, $query);
    
    $num_record = mysqli_num_rows ($risultato);
    if ($num_record!=1)
    {
         header('Location: ./index.php'); // Redirecting To Home Page per il login
         $con->close();
         exit; 
    }

I want it to send me back to the home when there isn't a session, but it will make a redirect loop.

CodePudding user response:

Don't do this check when you're already on index.php.

if($_SERVER['PHP_SELF'] != '/index.php' && !isset($_SESSION['username'])) 
{                                       
   header('Location: ./index.php'); // Redirecting To Home Page per il login
   exit;
}

CodePudding user response:

Use different session check at the index file like

if(isset($_SESSION['username'])){                                       
   
   header('Location: ./dashboard.php'); // Redirecting To Dasboard Page Already logged in
   exit;

}

  • Related