关于连接和登录的PHP OOP类中的错误

时间:2016-04-28 08:08:02

标签: php oop pdo

我正在关注PHP的在线课程,我必须解决一个将程序脚本转换为OOP的练习。该脚本是关于使用PDO连接到MySQL并创建一个连接和登录的类。

我有这个类来创建到db的连接:

var $db_host, $db_name, $db_user, $db_pass, $connection;



function __construct($host = "localhost", $name="sigmas", $user="root", $pass="ithss")
{
    $this->db_host = $host;
    $this->db_name = $name;

    $this->db_user = $user;
    $this->db_pass = $pass;
}

public function connect()
{
    $connection = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    return $connection;
}

function login($conn, $user, $pass, $msg='')
{
    $this->conn = $connetion;

    $this->username = $user;
    $this->password = $pass;
    if($username == "" || $password == "")
    {
        $this->msg = "Please add user name and password";
    }
    else
    {
        $sql = "SELECT * FROM login WHERE user_name = :user";
        $stmt = $conn->prepare($sql);
        $stmt->bindValue(':user', $username);
        $stmt->execute();
        $result = $stmt->fetchAll();
        if($result)
        {
            if(session_status() == PHP_SESSION_NONE)
            {
                session_start();
                foreach($result as $row)
                {
                    if(password_verify($password, $row['pass']))
                    {
                        $_SESSION['userid'] = $row['user_id'];
                        $_SESSION['role'] = $row['user_role'];
                        header("Location: index.php");              
                    }
                } 
            }
        }
        else
        {
            header("Location: wrong.php");
        }
    }
}

我需要将它包含在我的登录页面中:

include('classes.php');

if(isset($_POST['login_btn']))
{
    $username = $_POST['user_name'];
    $password = $_POST['user_pass'];

    $log = new myClass();
    $log->connect();
    $log->login($conn, $username, $password, $msg);
}

这是我的HTML表单:

<form class="form-signin" action="" method="POST">
                <input type="text" class="form-control" name="user_name" placeholder="Username" required autofocus>
                <input type="password" class="form-control" name="user_pass" placeholder="Password" required>
                <button class="btn btn-lg btn-primary btn-block" name="login_btn" type="submit">
                    Sign in</button>
        </form>

但我遇到了这些错误:

  

注意:未定义的变量:classes.php中的db_host

     

注意:未定义的变量:classes.php中的db_name

     

注意:未定义的变量:classes.php中的连接

     

注意:未定义的变量:classes.php中的用户名

2 个答案:

答案 0 :(得分:1)

$connection = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);

使用$this->db_user$this->db_pass之类的:

$connection = new PDO("mysql:host={$db_host};dbname={$db_name}", $this->db_user, $this->db_pass);

答案 1 :(得分:1)

connect()方法

中查看此行
$connection = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);

变量$db_host$db_name$db_userdb_pass在您的方法范围内不可用。

所以你的connect()方法应该是这样的:

public function connect()
{
    $this->connection = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
    $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    return $this->connection;
}

更新

首先,始终在PHP脚本的最顶层启动会话,如下所示:

<?php
    session_start();
    // your code

然后,声明必要的实例变量,而不是使用password_verify()函数,创建一个private实例方法来验证密码,如下所示:

private function password_verify($user_pass, $orig_pass){

    // Compare user's password with the original password

}

所以解决方案是:

保持HTML表单不变。提交后,处理您的表单:

session_start();
include('classes.php');

if(isset($_POST['login_btn'])){
    $username = $_POST['user_name'];
    $password = $_POST['user_pass'];

    $log = new myClass();
    $log->connect();
    if($log->login($username, $password)){

        // Login successful
        // redirect the user to a different page
        header("Location: process.php");
        exit();

    }else{
        // Incorrect username and/or password
        // display error message
        echo $log->msg;
    }
}

这里是修改过的 myClass 类,

class myClass{

    private $db_host;
    private $db_name; 
    private $db_user;
    private $db_pass;
    private $connection;
    public $msg;

    function __construct($host = "localhost", $name="sigmas", $user="root", $pass="ithss"){
        $this->db_host = $host;
        $this->db_name = $name;
        $this->db_user = $user;
        $this->db_pass = $pass;
    }

    public function connect(){
        $this->connection = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        return $this->connection;
    }

    public function login($user, $pass){
        if(empty($user) || empty($pass)){
            $this->msg = "Please fill the credentials";
        }else{
            $sql = "SELECT * FROM login WHERE user_name = :user LIMIT 1";
            $stmt = $this->connection->prepare($sql);
            $stmt->bindValue(':user', $user);
            $stmt->execute();
            $result = $stmt->fetchAll();
            if($result){
                $row = array_shift($result);
                if($this->password_verify($pass, $row['pass'])){
                    $_SESSION['userid'] = $row['user_id'];
                    $_SESSION['role'] = $row['user_role'];
                    return true;                    
                }else{
                    $this->msg = "Incorrect username and/or password";
                    return false;
                }
            }else{
                $this->msg = "Incorrect username and/or password";
                return false;
                //header("Location: wrong.php");
            }
        }
    }

    private function password_verify($user_pass, $orig_pass){

        // Compare user's password with the original password

    }

}