未定义的属性php

时间:2013-09-07 21:42:31

标签: php mysqli

您好我正在创建一个登录脚本但由于某种原因我在调用$mysqli时遇到了未定义的属性错误但是我在其他函数上以相同的方式调用它并且它运行良好例如我调用它$this->mysqli->prepare();这对我的插入函数有效。

然而,当在real_escape_string()的实例中使用它时,我实际上遇到了两个错误:

  

注意:未定义的属性:登录:: $ mysqli位于第28行的/Applications/XAMPP/xamppfiles/htdocs/imanage/class.Login.php

     

致命错误:在第28行的/Applications/XAMPP/xamppfiles/htdocs/imanage/class.Login.php中的非对象上调用成员函数real_escape_string()

编辑:第28行是指这一行 $safeUser = $this->mysqli->real_escape_string($user);

我真的不明白我做了什么不同的事情,关于如何解决这个问题的任何想法?

非常感谢任何输入和帮助

我的代码如下:

的index.php

<div id="maincontentWrapper">
<div id="maincontent">
    <div id="contentWrapper"></div><!--End loginWrapper -->
        <article>
            <p>Welcome to iManage, please login in below.</p>
        </article>
    <div id="loginform">
        <div id="loginWrapper">
        <form id="loginForm" method="POST" action="class.Login.php">
        <h1><span class="log-in">Log in</span> or <span class="sign-up"><a href="register">sign up</a></span></h1>
        <div id="errorDiv"><?php 
                    if (isset($_SESSION['error']) & isset($_SESSION['formAttempt'])) {
                            unset($_SESSION['formAttempt']);
                            print "Errors encountered<br/>\n";
                            foreach ($_SESSION['error'] as $error) {
                            print $error . "<br />\n";
                        } //end foreach
                        } //end if 
                ?></div>
    <p class="float">
        <label for="login"><i class="icon-user"></i>Username</label>
        <input type="text" id="email" name="email" placeholder="E-mail">
          <span class="errorFeedback errorSpan" id="emailError">E-mail is required</span>
    </p>
    <p class="float">
        <label for="password"><i class="icon-lock"></i>Password</label>
        <input type="password" id="password" name="password" placeholder="Password" class="showpassword"> 
                <span class="errorFeedback errorSpan" id="passwordError">Password is required</span>

    </p>
    <p class="clearfix"> 
        <input type="submit" name="submit" value="Log in"></form>
    </p>   
        </div>

    </div>


</div>
</div>

</div>

class.Login.php

<?php

include("connect/class.Connect.php");

class Login extends Database {

    public $id;
    public $email;
    public $username;

    function __construct() {

        if (session_id() == "") {
            session_start();    
        }

        if (isset ($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn'] == true) {
            $this->_initUser();
        } 

    } // end construct

    public function authenticate($user, $pass) {

        $user = isset($_POST['email']);
        $pass = isset($_POST['password']);

        $safeUser = $this->mysqli->real_escape_string($user);
        $incomingPassword = $this->mysqli->real_escape_string($pass);

        $query = "SELECT * from users WHERE email = '{$safeUser}'";

                if (!$result = $this->mysqli->query($query)) {
                        error_log("Cannot retrieve account for {$user}");
                        return false;
                }   

                // will be only one row, so no while() loop needed
                $row = $result->fetch_assoc();
                $dbPassword = $row['password'];

                if (crypt($incomingPassword,$dbPassword) != $dbPassword) {
                        error_log("Passwords for {$user} don't match");
                        return false;
                }
                    $this->id = $row['id'];
                    $this->username = $row['username'];
                    $this->email = $row['email'];
                    $this->isLoggedIn = true;

                    $this->_setSession();
                    return true;    

    } // end authenticate 

        private function _setSession() {

        if (session_id() == '') {
            session_start();    
        }

        $_SESSION['id'] = $this->id;
        $_SESSION['email'] = $this->email;
        $_SESSION['username'] = $this->username;
        $_SESSION['isLoggedIn'] = $this->isLoggedIn;

    } // end function setSession


    private function _initUser() {

        if (session_id() == '') {
            session_start();    
        }

        $this->id = $_SESSION['id'];
        $this->email = $row['email'];
        $this->username = $row['username'];
        $this->user_role = $row['user_role'];
        $this->isLoggedIn = $_SESSION['isLoggedIn'];

    } // end initUser

         function preventaccess () {
        if (!isset($_POST['submit'])) {
            die(header("Location: login.php"));
        }
    } // end prevent access 

     function validatelogin () {
                    $_SESSION['formAttempt'] = true;

        if (isset($_SESSION['error'])) {
            unset($_SESSION['error']);
        }

            $_SESSION['error'] = array();

            $required = array("email", "password");

            //Check required fields
            foreach ($required as $requiredField) {
                if (!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
                $_SESSION['error'][] = $requiredField . " is required.";
            }
            }


            if (!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
            $_SESSION['error'][] = "Invalid e-mail address";
            }

            if (count($_SESSION['error']) > 0) {
                die(header("Location: login.php")); 
            } else {
                $user = new User;
                if ($user->authenciate($_POST['email'], $_POST['password'])) {
                    unset($_SESSION['formAttempt']);    
                 die(header("Location: authenticated.php"));
            }else {
                 $_SESSION['error'][] = "There was a  problem with your username or password.";
                 die(header("Location: login.php"));
                }
        }
        } // end validate 

}
    $run = new Login();
    $run->__construct();
    $run->authenticate($_POST['email'],$_POST['password']);
    $run->validatelogin();
?>

连接/ class.Connect.php

<?php

/**
 * MySQLi database
 */
class Database {


        public function __construct(){

                $this->mysqli = new mysqli('localhost', 'root', '', 'imanage');

                if(mysqli_connect_errno()) {

                    echo "Error: Could not connect to database.";

                exit;

        }
        /*else{
            echo"Your Database successfully connected"; 
        }*/

    }

    public function __destruct(){
        $this->mysqli->close(); 
    }



}

2 个答案:

答案 0 :(得分:1)

class Login extends Database {
            ################
    ...

    function __construct() {

        ...

        parent::__construct();    <--- missing
        ######################

        ...

    } // end construct

    ...

答案 1 :(得分:0)

您的数据库类中没有定义属性mysqli。这应该

<?php

    /**
     * MySQLi database
     */
    class Database {

        private $_mysqli;
        public function __construct() {

            $this->_mysqli = new mysqli('localhost', 'root', '', 'imanage');

            if(mysqli_connect_errno()) {

                echo "Error: Could not connect to database.";

                exit;

            }
            /*else{
                echo"Your Database successfully connected"; 
            }*/

        }

        public function __destruct(){
            $this->_mysqli->close(); 
        }

    }
?>