douzao1119 2014-02-18 15:11
浏览 38

php crypt密码和postgresql数据库

I'm new in PHP. I'm doing authentication, where I'm checking password with password stored in database PostgreSQL. On db site i used this function to crypt my password:

update ucty set psswd =  crypt('some_pswd',gen_salt('md5')) where uid='1';

In my PHP srcipt I'm using this code:

$query = "SELECT meno, priezvisko, nickname, psswd, uid 
          FROM ucty 
          where nickname='$nickname' and psswd=crypt('$password', psswd)";

Everything works fine, but I'm not sure , that this is correct way to secure my password.

Any advice?

  • 写回答

1条回答 默认 最新

  • drasebt1835 2014-02-18 15:16
    关注

    You're correct; this isn't the correct way to secure your password.

    • You're encrypting the password as part of the query. This can be logged (in plaintext), so it's very possible for intruders (or anyone listening to your traffic) to see users' passwords in plaintext.

      "How can I prevent this?" Do your hashing on the server-side, within your PHP code. You can read up on this in the PHP manual.

      Essentially, you want to have your query to set a password be something like this:

      UPDATE ucty SET psswd=$hashed WHERE uid=1;
      
    • You're putting variables directly into the SQL statement. You didn't mention what method you're using to query the database, but you'll want to use prepared statements. This is a safe way to slide in user-supplied data (which $nickname and $password are).

      This would be an example of a good way to use prepared statements:

      $query = "SELECT meno, priezvisko, nickname, psswd, uid"
         . " FROM ucty"
         . " WHERE nickname=? and psswd=?";
      
      $stmt = $dbh->prepare($query);
      $stmt->execute(array($nickname, $hashedPassword));
      
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog