简体   繁体   English

(PHP)登录只被重定向到一页

[英](PHP)Login Only Gets Redirected To 1 Page

My admin page is The only one being accessed for both.我的管理页面是两者都可以访问的唯一页面。

my Code: <?php我的代码:<?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.当我登录到我的用户页面时,它会被重定向到管理页面。

When I try to adjust the code most of the time only 1 page works.当我大部分时间尝试调整代码时,只有 1 页有效。

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');

  }
  }
  ?>

I hope this will help you:我希望这能帮到您:

  1. Rearrange your user table:重新排列您的用户表:
    USER:
      -> id (int)
      -> username (string)
      -> password (string)
      -> isAdmin (bool)
  1. fire your Request:触发您的请求:
    $usernane = $_POST['username'];
    $password = sha1($_POST['password' ]);
    $stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1");
    $stmt->execute(array($usernane, $password));
  1. check the result检查结果
    if($stmt->rowCount() > 0) {
      $user = $stmt->fetch();
      if($user['isAdmin'])
      {
        $target = 'Admin.php';
      } else {
        $target = 'User.php';
      }
      header(sprintf('location:%s', $target));
      
    } else {
      // no user found with this credentials
    }

Be careful, I have not tested the code.小心,我没有测试过代码。

Next you have todo: never save clear passwords to the database.接下来你必须做的:永远不要将明确的密码保存到数据库中。 Take a look on the password tools of php and use that.查看 php 的密码工具并使用它。

https://www.php.net/manual/de/function.password-hash.php https://www.php.net/manual/de/function.password-hash.php

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');
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM