こんにちは、ログイン スクリプトを作成していますが、何らかの理由で呼び出し時に未定義のプロパティ エラーが発生しますが$mysqli
、他の関数でも同じように呼び出して正常に動作しました。たとえば、そのように呼び出しています。$this->mysqli->prepare();
これは私の挿入機能で機能しました。
ただし、インスタンスで使用すると、real_escape_string()
実際には2つのエラーが発生します。
Notice: 未定義のプロパティ: Login::$mysqli in /Applications/XAMPP/xamppfiles/htdocs/imanage/class.Login.php 行 28
致命的なエラー: 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();
}
}