0

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

但是,在实例中使用它时,real_escape_string()我实际上遇到了两个错误:

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

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

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

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

将不胜感激任何输入和帮助

我的代码如下:

索引.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>

类.登录.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();
?>

连接/类.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(); 
    }
  
  
        
}
4

2 回答 2

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

    function __construct() {

        ...

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

        ...

    } // end construct

    ...
于 2013-09-07T22:31:10.437 回答
0

您没有mysqli在 Database 类中定义属性。这应该做

<?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(); 
        }

    }
?>
于 2013-09-07T21:45:49.883 回答