title: php login page with different redirect
body: <p>I created login page linked to database and I want to redirect the user to different homepages based on the user role if 1 redirects to indexorg.php after login and if 0 redirects to indexpart.php after login however it is not working and all are redirected to same page  either indexorg.php or indexpart.php which is first in the code</p>
  <p>here is my code</p>
  <pre><code>&lt;?php
  
  // Initialize the session
  session_start();
   
  // Check if the user is already logged in, if yes then redirect him to welcome page
  if(isset($_SESSION[&quot;loggedin&quot;]) &amp;&amp; $_SESSION[&quot;loggedin&quot;] === true){
      if ($_SESSION[&quot;user_role&quot;] = &quot;1&quot;) {
          $redirect = 'indexorg.php';
      } else if ($_SESSION[&quot;user_role&quot;] == &quot;0&quot;) {
          $redirect = 'indexpart.php';
      } 
      
      header('Location: ' . $redirect);
      
  
  
  
      //header(&quot;location: index.php&quot;);
      exit;
  }
   
  // Include config file
  require &quot;config.php&quot;;
   
  // Define variables and initialize with empty values
  $email = $password = &quot;&quot;;
  $email_err = $password_err = &quot;&quot;;
   
  // Processing form data when form is submitted
  if($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;){
   
      // Check if email is empty
      if(empty(trim($_POST[&quot;email&quot;]))){
          $email_err = &quot;Please enter email.&quot;;
      } else{
          $email = trim($_POST[&quot;email&quot;]);
      }
      
      // Check if pass is empty
      if(empty(trim($_POST[&quot;password&quot;]))){
          $password_err = &quot;Please enter your password.&quot;;
      } else{
          $password = trim($_POST[&quot;password&quot;]);
      }
      
      // Validate credentials
      if(empty($email_err) &amp;&amp; empty($password_err)){
          // Prepare a select statement
          $sql = &quot;SELECT user_role, user_id, email, password  FROM users WHERE email = :email&quot;;
          
          if($stmt = $pdo-&gt;prepare($sql)){
              // Bind variables to the prepared statement as parameters
              $stmt-&gt;bindParam(&quot;:email&quot;, $param_email, PDO::PARAM_STR);
              
              // Set parameters
              $param_email = trim($_POST[&quot;email&quot;]);
              
              // Attempt to execute the prepared statement
              if($stmt-&gt;execute()){
                  // Check if email exists, if yes then verify password
                  if($stmt-&gt;rowCount() == 1){
                      if($row = $stmt-&gt;fetch()){
                          $id = $row[&quot;user_id&quot;];
                          $email = $row[&quot;email&quot;];
                          $hashed_password = $row[&quot;password&quot;];
                          $role = intval($row['user_role']);
                          if(password_verify($password, $hashed_password)){
                             
                              // Password is correct, so start a new session
                              session_start();
                              
                              // Store data in session variables
                              $_SESSION[&quot;loggedin&quot;] = true;
                              $_SESSION[&quot;user_id&quot;] = $id;
                              $_SESSION[&quot;email&quot;] = $email;
                              $_SESSION[&quot;user_role&quot;] == $role;
                              // if ($role=1) {
                              //     $redirect = 'indexorg.php';
                              // } else if ($role= 0) {
                              //     $redirect = 'indexpart.php';
                              // }
                              switch ($role) {
                                  case 0:
                                      $redirect= &quot;indexpart.php&quot;;
                                      break;
                                  case 1:
                                      $redirect= &quot;indexpart.php&quot;;
                                      break;
                                  
                              } 
                                                        
                              header('Location: ' . $redirect);
                              exit();
  
                              
                              // Redirect user to welcome page
                              //header(&quot;location: index.php&quot;);
                          } else{
                              // Display an error message if password is not valid
                              $password_err = &quot;The password you entered was not valid.&quot;;
                          }
                      }
                  } else{
                      // Display an error message if email doesn't exist
                      $email_err = &quot;No account found with that email.&quot;;
                  }
              } else{
                  echo &quot;Oops! Something went wrong. Please try again later.&quot;;
              }
  
              // Close statement
              unset($stmt);
          }
      }
      
      // Close connection
      unset($pdo);
  }
  ?&gt;
   
  </code></pre>