我正在嘗試添加用戶恢復其用戶名和/或密碼到phpacademy的OOP登錄/註冊教程的功能。我已經開始,但我不知道如何繼續。我擁有的代碼基於phpacademy的舊版非OOP登錄/註冊教程。恢復用戶名/密碼
下面的代碼: recover.php:
<?php
require_once 'core/init.php';
$user = new User();
if ($user->isLoggedIn()) {
Redirect::to('index.php');
}
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
?>
<p>We've emailed you</p>
<?php
} else {
$mode_allowed = array('username', 'password');
if (isset($_GET['mode']) === true && in_array($_GET['mode'], $mode_allowed) === true) {
if (isset($_POST['email']) === true && empty($_POST['email']) === false) {
$email = Input::get('email');
if ($user->emailExists($email) === true) {
$user->recover($_GET['mode'], $email);
Redirect::to('recover.php?success');
} else {
//Your custom error saying that that email does not exist
echo 'That email does not exist';
}
}
?>
<form action="" method="post">
<ul>
<li>
Please enter your email address:<br>
<input type="text" name="email" placeholder="Enter Email">
</li>
<li><input type="submit" class="sub-btn tx-shadow" value="Recover"></li>
</ul>
</form>
<?php
} else {
Redirect::to('index.php');
}
}
我在user.php的這些類:
public function emailExists($email){
$sql = "SELECT *
FROM `users`
WHERE `email` = ?
";
$result = $this->_db->query($sql, array($email));
//return $result->count() === 1;
// Above is the same as this:
//return ($result->count() === 1) ? true : false;
// Or this:
if($result->count() === 0){
return false;
} else {
return true;
}
}
public function recover($mode, $email) {
//Get data of user
$user_data = $this->getUserByEmail($email);
// Email User
if ($mode == 'username') {
$this->sendActivation($email, 'Your Username', 'Hello'.$user_data['first_name']. '\n\nYour username is: '.$user_data['username'].'\n\nThank you\nBusarna');
} else if ($mode == 'password') {
$generated_password = substr(sha1(rand(999, 999999)), 0, 10);
//update password
}
}
public function getUserByEmail($email) {
$sql = "SELECT *
FROM `users`
WHERE `email` = ?
"
$result = $this->_db->query($sql, array($email));
//return $result;
}
的emailExists功能工作。我的問題是,我不知道如何使用他們的電子郵件來獲取所有來自用戶的信息,並存儲信息使用它們像這樣:
$this->sendActivation($email, 'Your Username', 'Hello'.$user_data['first_name']. '\n\nYour username is: '.$user_data['username'].'\n\nThank you\nBusarna');
我上面,但我不類不知道如何改變它以使其工作(getUserByEmail)。此外,我不知道是否上面的方式是一個生成密碼的好方法(不加密,在放入數據庫時會被哈希),我不知道如何更新密碼並強制用戶更改密碼在下一次登錄。
現在我得到這個錯誤,如果我去recover.php:解析錯誤:語法錯誤,意外的'$結果'(T_VARIABLE)在D:\ Programme \ xampp \ htdocs \ website \ classes \ User.php上線225這是該行:
$result = $this->_db->query($sql, array($email));
如果你們需要什麼只是讓我知道。
db.php中類:
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach ($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if ($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
的var_dump($ USER_DATA);對象(DB)#3(5){[「_pdo」:「DB」:private] => object(PDO)#4(0){} [「_query」:「DB」:private] = > object(PDOStatement)#7(1){[「queryString」] => string(55)「SELECT * FROM users
WHERE email
=?」} [「_error」:「DB」:private] => bool(false) [「_results」:「DB」:private] => object(stdClass)#5(18){[「id」] => string(2)「28」[「username」] => string(8)「Busarna4 「[」 密碼 「] =>串(128) 」3759d763d01ee1259d8f4a74914335be4e3e06622d8ffc455789cbd8c46b5800d1892794cde2de1b0f6a1cf8e1effc642877b7937b7c0deaf9036e558474eb5c「[」 鹽 「] =>串(100)」 Àw¶10$ E!ºL-KO「Ö§òŸÔK* {áëRÌÕÆé³Xï«AA * U™ (20)「[email protected]」[「email_code」] =>字符串(32)「ae3e9ad790297c627a656157d5306d1e」msgid。 「[」k1「] =>字符串(64)」42a86fe29289c83a8d91a990e49b62c3866192c983e510 70df0c63697020b5ce」 [ 「K2」] =>串(64) 「d7827e522bbbdadfd14afd2edbae18d5a6f3c39a490154c178b2eec240d7b576」[ 「K3」] =>串(64) 「8051c951f1ec043d5297765adddd760e1f8506d1785dfdb55dabe564b6326b86」[ 「如first_name」] =>串(0) 「」[ 「的last_name」] => string(0)「」[「gender」] => string(4)「Male」[「profile」] => string(0)「」[「avatar」] => string(30)「images/default-Male -Avatar.png「[」lastlogin「] => string(19)」0000-00-00 00:00:00「[」joined「] => string(19)」2014-06-22 21:22:36 「[」group「] => string(1)」1「[」activated「] => string(1)」1「} [」_count「:」DB「:private] => int(1)}
有誰知道如何做到這一點?所有幫助讚賞。
由於提前,
Busarna
取消註釋'//返回$ result'在getUserByEmail()功能。 – Gudgip
我注意到,因爲那個班級,我得到一個錯誤。我在問題中添加了錯誤。並取消註釋,因爲這個錯誤@Gudgip – Busarna4
把一個';'放在第224行的末尾沒有做任何事情。 – Gudgip