dt250827 2013-09-26 17:49
浏览 70
已采纳

PHP PDO登录脚本不起作用。 包含错误

I´m not very familiar with security, therefore I rely on what I find on the internet. I found a site of someone who explains a bit what he does and how his method works. People may copy-paste it to ease things up. Though I do understand quite a lot, I couldn't come up with it myself (I'm pretty new to PHP/XHTML, etc.)

The website: How to store safely with PHP and MySQL

He uses PDO in his tutorial. And I am able to store the information in the database. But when I try to use the script in which he provides the code for actually logging in, though it seems it contains errors.

I've worked everything out and everything works fine, but the comparison of the hashed password with the inserted password (with the hash, etc.) does not work properly.

What is going on here?

Thanks in advance!

EDIT

People have been asking for the code so, here it is:

session_start();  
  require('config.php');

  // Setting up a connection
  $MyConnection = new PDO('mysql:host=*;dbname=*', $dbuser, $pass);

  // Retrieving information from form.
  $username = $_POST['username'];
  $password = $_POST['password'];

  $sth = $MyConnection->prepare("SELECT * FROM AMP_Users WHERE Username = :username LIMIT 1");
  $sth->bindParam(':username', $username);
  $sth->execute();

  $user = $sth->fetch(PDO::FETCH_OBJ);

  // Hashing the password with its hash as the salt returns the same hash
  if (crypt($password, $user->hash) == $user->hash) {
    echo 'You are now logged in. If we actually used sessions this time.';
  }

I will add a $_SESSION['name'] = $username, once the code starts to work. Until now I simply echo out if it worked out or not. And it doesn't show anything, so it doesn't work.

SECOND EDIT

Just as a quick update, the script provided by me, is the WHOLE script. Nothing is let out. (Except names of databases, etc.) Therefore I wonder if the problem may be that I don't use the hashing script of the saving the passwords into the database. Though I have put it in, it still doesn't respond. Am I still doing something wrong?

  • 写回答

4条回答 默认 最新

  • douyan6548 2013-09-26 18:11
    关注
    if($_POST):
    
         $name = $_POST['username'];
         $pass = crypt($_POST['password'], '$2a$07$Hd893nD39Jdjd48Jdh3nD$');
    
         $conn = new PDO('mysql:host=*; dbname=*', 'root', '');
         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);    
    
         $stmt  = $conn->prepare('SELECT * FROM user WHERE name = ? AND password = ?');
         $stmt->execute(array($name, $pass));
    
         if($stmt->rowCount() === 0){
             echo 'Your Username / Password is incorrect. Please try again';
         }else{
             echo 'login success';
         }
    
        endif;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面