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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100