登录函数无法进行身份验证,PHP

时间:2013-10-05 08:02:53

标签: php sql authentication login

您好我无法理解为什么我的身份验证会继续失败。我尝试了很多调试方法,我想我很难过。在我看来,我仍然是一个初学者,现在安全不是问题。我只是想让它发挥作用。

因此,对于我的注册脚本,我让用户输入用户名和密码

            $username  = $_POST['username'];
            $password  = $_POST['password'];

//发布保存的数据

            $createTable = $db->prepare("CREATE TABLE `$username` (Title varchar(40) ,
                                                    About   varchar(1000) , 
                                                    Chapter int(4),
                                                    Img varchar(100) , 
                                                    Url varchar(50)) ");

            $createTable->execute();

///为用户

创建一个表
            $Register = $db->prepare("INSERT INTO library (id, 

username, password) VALUES ($id ,
'$username','$password')");
            $Register ->execute();

//将数据插入库表。库具有用户和密码

                echo 'user sucessfully registered';

                $_SESSION['user'] = $username;
                $_SESSION['pass'] = $password;

我遇到的问题是对登录进行身份验证。 使用两个SESSION变量作为参数调用登录函数。

function login($SentUser , $SentPass)

      {

echo 'trying to log in '.'<br>';

    require_once 'Connection.php';
    $user = $SentUser;
    $pass = $SentPass;

    /// check data base for match user and pass
        $sth = $db->prepare('SELECT username , password from `library` where username = ? And password = ? ');

        $sth->bindParam(1, $user);
        $sth->bindParam(2, $pass);
        $sth->execute();


        while ($row = $sth->fetch(PDO::FETCH_ASSOC))
            {
        echo 'user : ' . $row['username'] , '<br>';
        echo 'pass: ' . $row['password'] , '<br>';
        echo '<br>';
            }
        .
        // fetchcolumn returns true false if select took a row or not.
if($sth->fetchColumn() != false) { /// This is where the code fails as it almost always goes to the else case.

            $_SESSION['user'] = $user;
            $_SESSION['pass'] = $pass;
            $_SESSION['passed'] = TRUE;
            echo 'Welcome '.$user.'<br>';}
        else
            {

                echo 'failed authenticate'.'<br>';

            }

为了更加清晰,这里是正在调用的connection.php。它基本上只是将我连接到数据库,给我$ db对象。

<?php


$config['db'] = array(
    'host' => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname' => 'mywebsite'
    );




$db = new PDO('mysql:host=' . $config['db']['host']. ';dbname=' . $config['db']['dbname'] , $config['db']['username'] , $config['db']['password']);

?>

1 个答案:

答案 0 :(得分:1)

您提到library表有用户/密码。因此,每个用户必须只有一行。更改如下代码:

   if($sth->rowCount() == 1) {
      //row exists, so user exists
      $row = $sth->fetch(PDO::FETCH_ASSOC);
      echo 'user : ' . $row['username'] , '<br>';
      echo 'pass: ' . $row['password'] , '<br>';
      echo '<br>';

      //set session
      $_SESSION['user'] = $user;
      $_SESSION['pass'] = $pass;
      $_SESSION['passed'] = TRUE;
      echo 'Welcome '.$user.'<br>';
   } else {
      //auth failed
      echo 'failed authenticate'.'<br>';
   }