将位于类中的函数调用到另一个文件

时间:2014-01-21 01:08:10

标签: php function

我一直在尝试调用此函数并从函数userdata($ id)获取$ id到函数retrieve_proposal_data($ job)。我所有的努力都不是。我继续得到调用未定义函数错误。第一个函数位于另一个文件和类中。我需要从该函数获取$ id并在我的SELECT语句WHERE client_id = $ id中调用它。我是php的新手,尤其是函数,并不太清楚这是怎么做的。我已经seacrhed这个网站,阅读php cookbook寻找答案,但无济于事。两个文件中还有其他功能,但对于这个问题,我认为不需要发布它们。无论如何有任何帮助?这是我的代码。

// File: login.php

<?php
require 'core/init.php';
$general->logged_in_protect();

if (empty($_POST) === false) {

    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    if (empty($username) === true || empty($password) === true) {
        $errors[] = 'Sorry, but we need your username and password.';
    } else if ($users->user_exists($username) === false) {
        $errors[] = 'Sorry that username doesn\'t exists.';
    } else if ($users->email_confirmed($username) === false) {
        $errors[] = 'Sorry, but you need to activate your account. 
                     Please check your email.';
    } else {
        if (strlen($password) > 18) {
            $errors[] = 'The password should be less than 18 characters, without spacing.';
        }
        $login = $users->login($username, $password);
        if ($login === false) {
            $errors[] = 'Sorry, that username/password is invalid';
        }else {
            session_regenerate_id(true);// destroying the old session id and creating a new one
            $_SESSION['id'] =  $login;
            header('Location: home.php');
            exit();
        }
    }
} 
?>

// File: users.php

    <?php 
class Users{

    private $db;

    public function __construct($database) {
        $this->db = $database;
    }   

    public function update_user($first_name, $last_name, $gender, $bio, $image_location, $id){

        $query = $this->db->prepare("UPDATE `users` SET
                                `first_name`    = ?,
                                `last_name`     = ?,
                                `gender`        = ?,
                                `bio`           = ?,
                                `image_location`= ?

                                WHERE `id`      = ? 
                                ");

        $query->bindValue(1, $first_name);
        $query->bindValue(2, $last_name);
        $query->bindValue(3, $gender);
        $query->bindValue(4, $bio);
        $query->bindValue(5, $image_location);
        $query->bindValue(6, $id);

        try{
            $query->execute();
        }catch(PDOException $e){
            die($e->getMessage());
        }   
    }

    public function change_password($user_id, $password) {

        global $bcrypt;

        /* Two create a Hash you do */
        $password_hash = $bcrypt->genHash($password);

        $query = $this->db->prepare("UPDATE `users` SET `password` = ? WHERE `id` = ?");

        $query->bindValue(1, $password_hash);
        $query->bindValue(2, $user_id);             

        try{
            $query->execute();
            return true;
        } catch(PDOException $e){
            die($e->getMessage());
        }

    }

    public function recover($email, $generated_string) {

        if($generated_string == 0){
            return false;
        }else{

            $query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email` = ? AND `generated_string` = ?");

            $query->bindValue(1, $email);
            $query->bindValue(2, $generated_string);

            try{

                $query->execute();
                $rows = $query->fetchColumn();

                if($rows == 1){

                    global $bcrypt;

                    $username = $this->fetch_info('username', 'email', $email); // getting username for the use in the email.
                    $user_id  = $this->fetch_info('id', 'email', $email);// We want to keep things standard and use the user's id for most of the operations. Therefore, we use id instead of email.

                    $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
                    $generated_password = substr(str_shuffle($charset),0, 10);

                    $this->change_password($user_id, $generated_password);

                    $query = $this->db->prepare("UPDATE `users` SET `generated_string` = 0 WHERE `id` = ?");

                    $query->bindValue(1, $user_id);

                    $query->execute();

                    mail($email, 'Your password', "Hello " . $username . ",\n\nYour your new password is: " . $generated_password . "\n\nPlease change your password once you have logged in using this password.\n\n-Example team");

                }else{
                    return false;
                }

            } catch(PDOException $e){
                die($e->getMessage());
            }
        }
    }

    public function fetch_info($what, $field, $value){

        $allowed = array('id', 'username', 'first_name', 'last_name', 'gender', 'bio', 'email'); // I have only added few, but you can add more. However do not add 'password' eventhough the parameters will only be given by you and not the user, in our system.
        if (!in_array($what, $allowed, true) || !in_array($field, $allowed, true)) {
            throw new InvalidArgumentException;
        }else{

            $query = $this->db->prepare("SELECT $what FROM `users` WHERE $field = ?");

            $query->bindValue(1, $value);

            try{

                $query->execute();

            } catch(PDOException $e){

                die($e->getMessage());
            }

            return $query->fetchColumn();
        }
    }

    public function confirm_recover($email){

        $username = $this->fetch_info('username', 'email', $email);// We want the 'id' WHERE 'email' = user's email ($email)

        $unique = uniqid('',true);
        $random = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),0, 10);

        $generated_string = $unique . $random; // a random and unique string

        $query = $this->db->prepare("UPDATE `users` SET `generated_string` = ? WHERE `email` = ?");

        $query->bindValue(1, $generated_string);
        $query->bindValue(2, $email);

        try{

            $query->execute();

            mail($email, 'Recover Password', "Hello " . $username. ",\r\nPlease click the link below:\r\n\r\nhttp://www.example.com/recover.php?email=" . $email . "&generated_string=" . $generated_string . "\r\n\r\n We will generate a new password for you and send it back to your email.\r\n\r\n-- Example team");           

        } catch(PDOException $e){
            die($e->getMessage());
        }
    }

    public function user_exists($username) {

        $query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ?");
        $query->bindValue(1, $username);

        try{

            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1){
                return true;
            }else{
                return false;
            }

        } catch (PDOException $e){
            die($e->getMessage());
        }

    }

    public function email_exists($email) {

        $query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email`= ?");
        $query->bindValue(1, $email);

        try{

            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1){
                return true;
            }else{
                return false;
            }

        } catch (PDOException $e){
            die($e->getMessage());
        }

    }

    public function register($username, $password, $email){

        global $bcrypt; // making the $bcrypt variable global so we can use here

        $time       = time();
        $ip         = $_SERVER['REMOTE_ADDR']; // getting the users IP address
        $email_code = $email_code = uniqid('code_',true); // Creating a unique string.

        $password   = $bcrypt->genHash($password);

        $query  = $this->db->prepare("INSERT INTO `users` (`username`, `password`, `email`, `ip`, `time`, `email_code`) VALUES (?, ?, ?, ?, ?, ?) ");

        $query->bindValue(1, $username);
        $query->bindValue(2, $password);
        $query->bindValue(3, $email);
        $query->bindValue(4, $ip);
        $query->bindValue(5, $time);
        $query->bindValue(6, $email_code);

        try{
            $query->execute();

            mail($email, 'Please activate your account', "Hello " . $username. ",\r\nThank you for registering with us. Please visit the link below so we can activate your account:\r\n\r\nhttp://www.example.com/activate.php?email=" . $email . "&email_code=" . $email_code . "\r\n\r\n-- Example team");
        }catch(PDOException $e){
            die($e->getMessage());
        }   
    }

    public function activate($email, $email_code) {

        $query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email` = ? AND `email_code` = ? AND `confirmed` = ?");

        $query->bindValue(1, $email);
        $query->bindValue(2, $email_code);
        $query->bindValue(3, 0);

        try{

            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1){

                $query_2 = $this->db->prepare("UPDATE `users` SET `confirmed` = ? WHERE `email` = ?");

                $query_2->bindValue(1, 1);
                $query_2->bindValue(2, $email);             

                $query_2->execute();
                return true;

            }else{
                return false;
            }

        } catch(PDOException $e){
            die($e->getMessage());
        }

    }


    public function email_confirmed($username) {

        $query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ? AND `confirmed` = ?");
        $query->bindValue(1, $username);
        $query->bindValue(2, 1);

        try{

            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1){
                return true;
            }else{
                return false;
            }

        } catch(PDOException $e){
            die($e->getMessage());
        }

    }

    public function login($username, $password) {

        global $bcrypt;  // Again make get the bcrypt variable, which is defined in init.php, which is included in login.php where this function is called

        $query = $this->db->prepare("SELECT `password`, `id` FROM `users` WHERE `username` = ?");
        $query->bindValue(1, $username);

        try{

            $query->execute();
            $data               = $query->fetch();
            $stored_password    = $data['password']; // stored hashed password
            $id                 = $data['id']; // id of the user to be returned if the password is verified, below.

            if($bcrypt->verify($password, $stored_password) === true){ // using the verify method to compare the password with the stored hashed password.
                return $id; // returning the user's id.

            }else{
                return false;   
            }

        }catch(PDOException $e){
            die($e->getMessage());
        }

    }

    public function userdata($id) {

        $query = $this->db->prepare("SELECT * FROM `users` WHERE `id`= ?");
        $query->bindValue(1, $id);
print_r($id);
        try{

            $query->execute();

            return $query->fetch();

        } catch(PDOException $e){

            die($e->getMessage());
        }

    }

    public function get_users() {

        $query = $this->db->prepare("SELECT * FROM `users` ORDER BY `time` DESC");

        try{
            $query->execute();
echo($query); 
        }catch(PDOException $e){
            die($e->getMessage());
        }

        return $query->fetchAll();

    }   
}

然后

// File: retrieval.php

function retrieve_proposal_data($job) {
  $query = "SELECT * FROM client WHERE client_id = $id";
  $stuff = $db->prepare($query);
  $stuff->execute();
  $clientsdata = $stuff->fetch();
}

1 个答案:

答案 0 :(得分:0)

它在一个类中,因此需要实例化类,然后才能调用该方法。或者将其更改为静态方法(公共静态函数...)并调用Users :: userdata($ id);

您的最终选择是将方法移出课程,然后您就可以使用userdata($ id);以你期待的方式。

$Users = new Users();
$userdata = $Users->userdata($id);