dongyou5098 2015-09-02 19:39
浏览 50

php crypt生成不同的值[重复]

I'm have a problem with php crypt() function, it generates a different passwords

here is the code for adding a test user to the database:

    $hash = crypt('roms','rc');
    $data = array('username' => 'roms', 'password' => $hash, 'email' => 'roms@r.c');
    $this->db->insert('users',$data);

And here is the login function:

// user login function
public function userLogin($username, $password){
    $results = $this->db->select('select * from users where username = :username',
    array(':username'=>$username));

    //  print_r($results);
      $pass = $results[0]->password;
    //  echo '<br>';
    //  echo  crypt('roms','rc');
    //  echo '<br>';
    //  echo $password;
    //  echo '<br>';
    //  echo $pass;

    if( (count($results) > 0) ){
        //echo ">0";
        if($pass == $password){
            return true;
        }
    }
    return false;
}

The result of the function: it works but never passes the if($pass == $password) condition because the passed hash is different than the one stored in the database

// print_r($results);
Array ( [0] => stdClass Object ( [id] => 7 [username] => roms [password] => rc7y3Ie22wNUQ [email] => roms@r.c ) ) 

// echo crypt('roms','rc'); (crypt the original password)
rc7y3Ie22wNUQ

// echo $password; (passed to the function)
rcKGHUlyQfgrU

// echo $results[0]->password; (from the database)
rc7y3Ie22wNUQ

And here is the login function call

if(isset($_POST['user-login'])){

    $db = new DbHelpers();

    $username = $_POST['username'];
    $password = crypt($_POST['passowrd'],'rc');

    $bool = $db->userLogin($username, $password);
    if($bool){
        //echo "<script>alert('1');</script>";
        $_SESSION["username"] = $username;
        \Helpers\Url::redirect('admin');
    }else{
        //echo "<script>alert('0');</script>";
        $data['login-error'] = '1';
    }
}

Note that I'm using simple mvc framework, it has a Password class for hashing passwords but I had the same problem when I used it. I also tried the md5 function.

</div>
  • 写回答

1条回答 默认 最新

  • doufu5401 2015-09-02 19:43
    关注

    This is because the variable is spelled incorrectly, and the hash you're generating is for an empty string

    $password = crypt($_POST['passowrd'], 'rc');
    

    Should probably be:

    $password = crypt($_POST['password'], 'rc');
    

    When I run:

    echo crypt('', 'rc')
    

    It outputs: rcKGHUlyQfgrU

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测