dongliaojing0554 2012-12-25 21:53
浏览 33
已采纳

PHP登录脚本 - 如果语句和数组

Hello fellow programmers and coders. I am developing an enhanced login script based on the already great login script by zubrag.

What i am trying to achieve: If the user is an admin, he will be logged in. If the user is a regular user, he will not be logged in.

What happens: The user gets logged in, even if he is not an admin.

Here is a snippet of the code:

// user provided password
if (isset($_POST['access_password'])) {

  $login = isset($_POST['access_login']) ? $_POST['access_login'] : '';
  $pass = $_POST['access_password'];
  if (!USE_USERNAME && !in_array($pass, $users)
  || (USE_USERNAME && ( !array_key_exists($login, $users) || $users[$login] != $pass ) ) 
  ) {
      showLoginPasswordProtect("Incorrect username or password.");
  }

The Part that i am having trouble with:

  elseif (array_key_exists($login, $admins)) {
  showLoginPasswordProtect("User not an admin.");
  }

Rest of the code:

  else {
      // set cookie if password was validated
      setcookie("verify", md5($login.'%'.$pass), $timeout, '/');
      setcookie("user", $login, 0, '/');
      // Some programs (like Form1 Bilder) check $_POST array to see if parameters passed
      // So need to clear password protector variables
      unset($_POST['access_login']);
      unset($_POST['access_password']);
      unset($_POST['Submit']);
  }

}

Username and password array:

$users = array(
  'username' => 'password',
  'administrator' => 'administrator-password'
);

Admin array:

$admins = array(
  'administrator'
);

Now picture all of that together, and what would the problem be? (It's probably really easy, i'm just not a professtional coder.)

  • 写回答

1条回答 默认 最新

  • drxdn40242 2012-12-25 21:59
    关注

    Are you sure that admins can login?

    Here you are saying that if the username is in the admin array, deny the acces:

    elseif (array_key_exists($login, $admins)) {
      showLoginPasswordProtect("User not an admin.");
    }
    

      What you probably want to do is deny the acces for users not in the admin array:

    elseif (!array_key_exists($login, $admins)) {
      showLoginPasswordProtect("User not an admin.");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度