【问题标题】:How to use multiple password in php?如何在php中使用多个密码?
【发布时间】:2018-06-17 08:42:24
【问题描述】:

我有一个受密码保护的网页,只有一个密码。但是,我想在那个网页上使用多个密码......我对 PHP 很陌生...... 那么,谁能给我一些关于如何做到这一点的建议! 提前致谢! 这是我正在使用的代码:

  <?php
        $password = "anything";
  ?>

在该代码上,我想同时使用“anything”并同时使用“mypass”作为密码! 我只是使用此代码来乘以通行证。但是,行不通!

  <?php
        $password = "anything" + "mypass";
  ?>

&完整代码为:

    <?php 
    if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
    ?>

###  anything to hide before password given!

<?php 
}
else
{
if (isset($_POST['password']) || $password == "") {
  print "<p align=\"center\"><font color=\"red\"><b>Wrong Password !!!</b><br>Please enter the correct Password</font></p>";}
  print "<form method=\"post\"><p align=\"center\"><b>Please enter the Password</b><br/><br/>";
  print "<b>Password </b><input class=\"box\" name=\"password\" type=\"password\" maxlength=\"10\"><input class=\"button\" value=\"Download\" type=\"submit\"></p></form>";
}
?>

【问题讨论】:

  • 那个网页上有多个密码是什么意思?就目前而言,您的问题尚不清楚
  • 最好使用现有的login framework/library 或查找php认证教程
  • 在该代码上,我想使用“anything”并同时使用“mypass”作为密码!我只是使用此代码来乘以通行证。但是,行不通!
  • 使用数组,然后检查密码是否存在于数组中。
  • 不工作! #Qirel

标签: php


【解决方案1】:

使用一组密码。顺便说一句

===== 容易受到timing attacks 的攻击,请改用hash_equals,但即使是hash_equals 也容易受到if the length of the 2 inputs is not equal 的定时攻击,所以在比较之前对密码进行哈希处理,以填充长度。像

$authed=false;
if (isset($_POST["password"])){
    $passwords=array('pass1','pass2','pass3');
    $u=hash('md5',(string)$_POST["password"],true); // using a weak (and fast) CS hash is not a problem, because we're only using it to pad the length so we're not vulnerable to a timing attack. 
    foreach($passwords as $pass){
        if(hash_equals($u,hash('md5',$pass,true))){
            $authed=true;
            break;
        }
    }
}
// here $authed is true if a correct password was supplied

【讨论】:

    【解决方案2】:

    我想你想这样做

    $passwordList = ['password','secretPassword','anything'];
    if (isset($_POST["password"]) && in_array($_POST["password"], $passwordList)) {
        // do protected stuff here
    }
    

    但是。有一个大但是

    永远不要在代码中存储密码。为此使用数据库。

    我建议你按照Laracasts 的教程来熟悉 PHP

    【讨论】:

    • 警告,in_array 不是定时攻击安全的,这段代码容易受到定时攻击。
    • 如果你使用这种方法,黑客可以从测量页面加载时间中提取你的密码,不要将这种方法用于任何重要的东西(甚至不是重要的密码),黑客可以获取你的密码,并访问无论您想保护什么。
    • 非常感谢...它正在工作!!!但是,这里有另一个改进:>> if (isset($_POST['password']) || $password == "") {
    • 正如人们所说,这种类型的安全检查很容易受到攻击。尝试在互联网上搜索更好的方法
    • @hanshenrik,请提供替代解决方案
    【解决方案3】:

    一次尝试:

    $password = "anything";
    $password = $password . "mypass";
    //anythingmypass
    
    if (isset($_POST["password"]) && $_POST["password"] != $password) {
    
    print '<p align="center"><font color="red"><b>Wrong Password !!!</b><br>Please enter the correct Password</font></p>';
    } else if (isset($_POST['password']) && $_POST['password']==$password) {
    echo "password correct";
    }
    print '<form method="post"><p align="center"><b>Please enter the Password</b><br/><br/>';
    print '<b>Password </b><input class="box" name="password" type="password" maxlength="' . strlen($password).'"><input class="button" value="Download" type="submit"></p></form>';
    

    【讨论】:

    • md5() 太弱了。有处理密码的PHP函数
    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2012-09-21
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多