[英]Learning PHP OOP Forgot Password
希望你的假期一切顺利。 我正在尝试了解如何在实际意义上使用 OOP 来处理忘记密码表单(标准输入 email,将其放入 mysql 并发送电子邮件)。 我已经对程序方法有了理解和工作版本。 但是当我试图慢慢地重新编码我的网站的某些部分时,它们是面向对象的。
forgot_password.php
<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//include database file
require_once('database.php');
require_once('functions.php');
require_once('includes/classes/user.php');
$user = New User;
//set Variables to empty
$error_message = "";
$success_message = "";
//check if form has been submitted
if($_POST){
$email = test_input($_POST['email']);
$user->forgot_password($email);
}
?>
<html>
<head><title>Forgot Password</title></head>
<body>
<form action="forgot_password.php" method="POST">
<strong>Email</strong> <input type="text" name="email"> <br>
<input type="submit" value="Request New Password">
</form>
<?php echo $user->message; ?>
</body>
</html>
<?php
class User{
var $email;
var $conn;
var $message;
function __construct(){
$servername = "servername";
$username = "username";
$password = "password";
$database = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
}
function forgot_password($email){
//query for userid, fname, lname, email based off of email they entered
$sql = "SELECT user_id, first_name, last_name, email FROM users WHERE email='$email'";
$result = $conn->query($sql);
//if rows == 1 (email found) then insert into password_reset
if($result->num_rows == 1){
$row = $result->fetch_assoc();
//delete old records so no confusion
$sql = "DELETE FROM password_reset WHERE user_id='$row[user_id]'";
$this->query($sql);
$timestamp = time();
$token = bin2hex(random_bytes(10));
//insert into password_reset table
$sql = "INSERT INTO password_reset (user_id, random_token, date_created) VALUES ('$row[user_id]','$token','$timestamp')";
$result = $conn->query($sql);
//insert email into email queue
$message = "Hi $row[first_name] $row[last_name], <br>
?token=$token\">Reset Password</a>";
$sql = "INSERT INTO email_queue (first_name, last_name, email, message, priority, date_created) VALUES ('$row[first_name]','$row[last_name]','$row[email]','$message','2','$timestamp')";
$result = $conn->query($sql) or die($conn->error);
$this->message = "Please check your email for further instructions.";
}
//else let them know email wasnt found
else{
$this->message = "Sorry there was an error. ";
}
}
}
?>
这是错误:注意:未定义变量:第 19 行 /includes/classes/user.php 中的 conn
Fatal error: Uncaught Error: Call to a member function query() on null in /includes/classes/user.php:19 Stack trace: #0 /forgot_password.php(21): User->forgot_password('asdf') # 1 {main} 在第 1 行的 /includes/classes/user.php 中抛出
您收到此错误,因为您的所有 class 变量都需要按以下方式声明:
$this->servername = "servername";
$this->username = "username";
$this->password = "password";
$this->database = "database";
// Create connection
$this->conn = new mysqli($servername, $username, $password, $database);
然后你应该在其他功能中以相同的方式使用它们。 $this 指的是 class 的当前实例。 没有它,变量会在 function 的 scope 之外丢失。 这就是为什么你会得到“呼叫成员 function query() on null”,因为它认为 $conn 是 null。
“this”的概念不仅在 PHP 中使用,而且在许多其他语言中也使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.