恢复用户名/密码

时间:2014-07-18 10:19:06

标签: php oop pdo

我正在尝试添加用户恢复用户名和/或密码到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)。另外,我不知道上面的方法是否是一个生成密码的好方法(没有,当放入数据库时​​会被散列),我不知道如何更新密码并强制用户更改密码在下次登录时。

现在我得到这个错误,如果我去恢复.php:解析错误:语法错误,D:\ Program \ xampp \ htdocs \ website \ classes \ User中的意外'$ result'(T_VARIABLE)。第225行的PHP 这就是这一行:

$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);

object(DB)#3(5){[“_ pdo”:“DB”:private] =&gt; object(PDO)#4(0){} [“_query”:“DB”:private] =&gt; object(PDOStatement)#7(1){[“queryString”] =&gt; string(55)“SELECT * FROM users WHERE email =?”} [“_ error”:“DB”:private] =&gt; bool(false)[“_results”:“DB”:private] =&gt; object(stdClass)#5(18){[“id”] =&gt; string(2)“28”[“username”] =&gt; string(8)“Busarna4”[“password”] =&gt; string(128)“3759d763d01ee1259d8f4a74914335be4e3e06622d8ffc455789cbd8c46b5800d1892794cde2de1b0f6a1cf8e1effc642877b7937b7c0deaf9036e558474eb5c”[“salt”] =&gt;字符串(100)“Àw¶10$ê!ºL-Kó”Ö§òŸÔK* {áëRÌÕÆé³Xïâ€Â*Ú™ü²ÅK§þ3hõHï¬Å+ c | k |á³³Trë_ßÙáû,ð¡_Ë<Žz0“[”email “] =&GT; string(20)“busarna4@hotmail.com”[“email_code”] =&gt; string(32)“ae3e9ad790297c627a656157d5306d1e”[“k1”] =&gt; string(64)“42a86fe29289c83a8d91a990e49b62c3866192c983e51070df0c63697020b5ce”[“k2”] =&gt; string(64)“d7827e522bbbdadfd14afd2edbae18d5a6f3c39a490154c178b2eec240d7b576”[“k3”] =&gt; string(64)“8051c951f1ec043d5297765adddd760e1f8506d1785dfdb55dabe564b6326b86”[“first_name”] =&gt; string(0)“”[“last_name”] =&gt; string(0)“”[“gender”] =&gt; string(4)“Male”[“profile”] =&gt; string(0)“”[“avatar”] =&gt; string(30)“images / default-Male-Avatar.png”[“lastlogin”] =&gt; string(19)“0000-00-00 00:00:00”[“加入”] =&gt; string(19)“2014-06-22 21:22:36”[“group”] =&gt; string(1)“1”[“activated”] =&gt; string(1)“1”} [“_ count”:“DB”:private] =&gt; int(1)}

有谁知道怎么做?所有帮助表示赞赏。

提前致谢,

Busarna

2 个答案:

答案 0 :(得分:3)

public function getUserByEmail($email) {
    $sql = "SELECT *
            FROM `users`
            WHERE `email` = ?
            ";
    //       ^-------------------------  missing semicolon

    $result = $this->_db->query($sql, array($email));

    // Don't forget to return the result:
    return $result;
}

unexpected '$result'意味着,它会看到$ result,但它确实期望$ result之前的行。带有$ result的行之前的命令未完成。

答案 1 :(得分:2)

<强>问题:

在您的函数query()中,您使用fetchAll即使您获得单个记录也将返回二维数组。

<强>解决方案:

  1. 使用fetch()代替单条记录。
  2. 更改功能:

    public function query_single($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->fetch(PDO::FETCH_ASSOC:);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }
    
        return $this->_results;
    }
    

    然后将其他功能更改为

    public function getUserByEmail($email) {
        $sql = "SELECT *
                FROM `users`
                WHERE `email` = ?
                ";
    
        $result = $this->_db->query_single($sql, array($email));
    
        return $result;
    }
    

    最后

    $this->sendActivation($email, 
                          'Your Username', 
                          'Hello'.$user_data['username'] 
                          .'\n\nYour username is: '.$user_data['username']
                          .'\n\nThank you\nBusarna');