Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(PHP)Login Only Gets Redirected To 1 Page

My admin page is The only one being accessed for both.

my Code:
<?php

  if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST')
  {
  $usernane = $_POST['username'];
  $password = sha1($_POST['password' ]);
  $stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1");
  $stmt->execute(array($usernane, $password));
  $checkuser = $stmt->rowCount();
  $user = $stmt->fetch();

When Ever I log into my User page it gets redirected to the Admin Page.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I try to adjust the code most of the time only 1 page works.

Any help will be appreciated!

  if ($checkuser === 0)
  {
  $_SESSION[ 'user' ] = $user['username'];
  $_SESSION[ 'type'] = $user['type'];
  header('location:User.php');

  }if ($checkuser === 1)
  {
  $_SESSION[ 'user' ] = $user['username'];
  $_SESSION[ 'type'] = $user['type'];
  header('location:Admin.php');

  }
  }
  ?>

>Solution :

In the second part of your code you are checking whether the user exists or not, if you want to seperate admins from normal users, you need to check the usertype instead of row count:

/*----- Check User Exists? ------*/
    if ($checkuser === 0){
        // This area will execute when user not exists.
    }else if($checkuser === 1){ // I suggest to add 'else' in here 

        /*----- Get user Data ------*/
            $_SESSION[ 'user' ] = $user['username'];
            $_SESSION[ 'type'] = $user['type'];

    /*----- Check Privilage In here ------*/
        if($user['type'] == 1){ // If your admin type == 1 then use it else change '1' in here
            header('location:Admin.php');
        }else{
            header('location:User.php');
        }

    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading