Skip to content
Advertisement

For some reason. IF statement is working….Ive tried If/else with no luck either [closed]

Only the ‘Admin’ if statement runs correctly. Anything else will jump to the un-nested else…any reason as to why?

Note: Yes, I’ve checked the spelling of the ranks…they are spelled correctly

Will redirect after user has succesfully logged in (login-sucessful.php):

<?php
  session_start();

  if(isset($_SESSION["username"])){
    if ($_SESSION["rank"] == 'Admin'){//Check if Admin
      header("location:/panel/admin/profile.php");
    } elseif ($_SESSION["rank"] == 'Faculty'){//Check if Faculty
      header("location:/panel/student/profile.php");
    } elseif ($_SESSION["rank"] == 'Student'){//Check if Student
      header("location:/panel/faculty/profile.php");
    }
  }else{
    header("location:log-in.php");
  }
 ?>

login page (login.php):

<?php
session_start();
$host = "localhost";
$username = "username";
$password = "password!";
$database = "database";
$message = "";
try
{
     $connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);
     $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     if(isset($_POST["login"]))
     {
          if(empty($_POST["username"]) || empty($_POST["password"]))
          {
               $message = '<label class="red-text text-darken-2">All fields are required</label>';
          }
          else
          {
               $query = "SELECT * FROM login WHERE username = :username AND password = :password";
               $statement = $connect->prepare($query);
               $statement->execute(
                    array(
                         'username'     =>     $_POST["username"],
                         'password'     =>     $_POST["password"]
                    )
               );
               $count = $statement->rowCount();
               if($count > 0)
               {
                    $_SESSION["username"] = $_POST["username"];
                    $_SESSION["rank"] = $statement->fetchColumn(3);
                    header("location:login_success.php");

               }
               else
               {
                    $message = '<label class="red-text text-darken-2">Wrong Email or Password</label>';
               }
          }
     }
}
catch(PDOException $error)
{
     $message = $error->getMessage();
}
?>

Login Table: Login Table

Advertisement

Answer

Wow….Cant believe I just spent an hour on this….its 2am and im exhausted…must be the reason this slipped up…. } elseif ($_SESSION[“rank”] == ‘Faculty’){//Check if Faculty header(“location:/panel/student/profile.php”); } elseif ($_SESSION[“rank”] == ‘Student’){//Check if Student header(“location:/panel/faculty/profile.php”); files linked to wrong directory which forced it to shoot back to login page (cause rank wasnt allowed) Thanks so much guys…I feel like a dumbass…Going to sleep now

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement