用php进行验证

用php进行验证,php,codeigniter,Php,Codeigniter,我试图用php验证我的用户服务器端,它说我遇到了一个致命错误:调用未定义的函数reGenPassHash()在第13行的/home/xtremer/public_html/kowmanager/application/models/loggedin.php中,现在我有了一个包含reGenPassHash函数的模型,该函数已自动加载,因此我认为它可以使用,但出于某种原因,这并不是因为这个消息。有人解释为什么 型号: public function check_login($username, $p

我试图用php验证我的用户服务器端,它说我遇到了一个致命错误:调用未定义的函数reGenPassHash()在第13行的/home/xtremer/public_html/kowmanager/application/models/loggedin.php中,现在我有了一个包含reGenPassHash函数的模型,该函数已自动加载,因此我认为它可以使用,但出于某种原因,这并不是因为这个消息。有人解释为什么

型号:

public function check_login($username, $password)
{
    $generated_password = reGenPassHash($password);
    $query = "SELECT user_id WHERE username = ? AND password = ?";
    $result = $this->db->query($query, array($username, $generated_password));

    if ($result->num_rows == 1)
    {
        return $result->row(0)->user_id;

    }
    else
    {
        return false;
    }

}
控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Usermanagement extends CI_Controller { 

public function __construct()
{
    parent::__construct();
}   

public function index()
{
    //Config Defaults Start
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg
    $cssPageAddons = '';//If you have extra CSS for this view append it here
    $jsPageAddons = '';//If you have extra JS for this view append it here
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's
    $siteTitle = '';//alter only if you need something other than the default for this view.
    //Config Defaults Start


    //examples of how to use the message box system (css not included).
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...');

    /**********************************************************Your Coding Logic Here, Start*/

    if(!$this->session->userdata('logged_in'))
    {
        $bodyContent = "login";//which view file
    }
    else
    {
        $bodyContent = "cpanel/index";//which view file
    }

    $bodyType = "full";//type of template

    /***********************************************************Your Coding Logic Here, End*/

    //Double checks if any default variables have been changed, Start.
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.      
    if(count($msgBoxMsgs) !== 0)
    {
        $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs));
    }
    else
    {
        $msgBoxes = array('display' => 'none');
    }

    if($siteTitle == '')
    {
        $siteTitle = $this->metatags->SiteTitle(); //reads 
    }

    //Double checks if any default variables have been changed, End.

    $this->data['msgBoxes'] = $msgBoxes;
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view.
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view.
    $this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view.
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
    $this->data['bodyType'] = $bodyType;
    $this->data['bodyContent'] = $bodyContent;
    $this->load->view('usermanagement/index', $this->data);
}

function login()
{
    $this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[50]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[12]|xss_clean');

    if ($this->form_validation->run() == FALSE)
    {
        $this->index();
    }
    else
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $user_id = $this->loggedin->check_login($username, $password);

        if(! $user_id)
        {
           redirect('/'); 
        }
        else
        {
            $this->session->set_userdata(array(
                'logged_in' => TRUE,
                'user_id' => $user_id
            ));
            redirect('cpanel/index');
        }
    }
}

function logout()
{
   $this->session->sess_destroy();
   $this->index();
}       

}

/* End of file usermanagement.php */ 
/* Location: ./application/controllers/usermanagement.php */ 
型号:

$this->your_model_with_pass_function->reGenPassHash()

自动加载只会对对象方法起作用。在对regen函数的调用中没有使用对象表示法,因此它被视为常规函数调用-并且该函数没有定义。

自动加载只会对对象方法起作用。您对regen函数的调用没有使用对象表示法,因此它被视为常规函数调用-并且此函数未定义。

您需要通过模型对象调用reGenPassHash函数,如:


您需要通过模型对象调用reGenPassHash函数,如:


我在代码中看到的唯一错误是,以错误的方式调用方法。对于任何类方法,都需要一个对象来访问这些方法

这可能是一美元。在你的情况下,或者在其他情况下

尝试使用
$this->reGenPassHash()
调用方法,仅当该方法依赖于模型中的时,,否则您将需要相应的对象修饰符

更新:

  • 将GenPassHash()和reGenPassHash()包含在控制器上
  • 不要使用
    $this->getfunc->reGenPassHash()
    而使用
    $this->reGenPassHash()

  • 我在代码中看到的唯一错误是,以错误的方式调用方法。对于任何类方法,都需要一个对象来访问这些方法

    这可能是一美元。在你的情况下,或者在其他情况下

    尝试使用
    $this->reGenPassHash()
    调用方法,仅当该方法依赖于模型中的时,,否则您将需要相应的对象修饰符

    更新:

  • 将GenPassHash()和reGenPassHash()包含在控制器上
  • 不要使用
    $this->getfunc->reGenPassHash()
    而使用
    $this->reGenPassHash()
  • reGenPassHash()函数对check_login()方法不可见。使用对象调用reGenPassHas

    例如:
    $object->reGenPassHas()

    这是一个范围问题,check\u login()方法无法看到reGenPassHash()函数。使用对象调用reGenPassHas

    例如:
    $object->reGenPassHas()



    这是一个范围问题

    能否向我们展示reGenPassHash()函数所在模型的代码?你确定要在check_login函数上面包含该文件吗?如果它是自动加载的,这不意味着它包含在这个模型上面。你能告诉我们reGenPassHash()函数所在的模型的代码吗?你确定要在check_login函数上面包含该文件吗?如果它是自动加载的,这不意味着它包含在这个模型上面。我可以从这个模型里面的另一个模型调用一个函数吗?所以你说它应该是$generated_password=$this->model->getfunc->reGenPassHash($password);而不是$generated\u password=RegentPassHash($password);我可以从这个模型中的另一个模型调用函数吗?所以你说它应该是$generated\u password=$this->model->getfunc->reGenPassHash($password);而不是$generated\u password=RegentPassHash($password);是的,从逻辑上讲,您的模型应该只包含与数据库相关的逻辑。。。。函数reGenPassHash()仅与开发哈希代码相关,与数据库无关。。。所以代码应该放在controller中:)谢谢你的澄清。我想我仍然停留在这个问题上,因为我应该如何实现我的目标。我将用一些更新的东西再次更新我的帖子,看看它对你是否有意义。是的,从逻辑上讲,你的模型应该只包含与数据库相关的逻辑。。。。函数reGenPassHash()仅与开发哈希代码相关,与数据库无关。。。所以代码应该放在controller中:)谢谢你的澄清。我想我仍然停留在这个问题上,因为我应该如何实现我的目标。我将用一些更新的内容再次更新我的帖子,看看它对您是否有意义。是的,最好的做法是在Controloer中使用
    reGenPassHas()
    ,因为它不会处理您的数据库。如果您的方法要使用DB,最佳实践是将它们移动到模型中。是的,最好的做法是将
    reGenPassHas()
    放在Controller中,因为它不会与您的数据库交互。如果您的方法要使用DB,最佳实践是将它们移动到模型中。您的代码应该像您现在编辑的那样工作,但正如其他帖子所说,如果您需要多个控制器才能访问这些函数,您应该将密码函数移动到您的模型中,或者移动到库中。实际上,您的模型是一个库。只需将它移动到库中并作为库自动加载,您将以与新代码相同的方式调用它。它仍然不起作用吗?你的代码应该像你现在编辑的那样起作用,但正如其他帖子所说的那样,如果你需要多个控制器才能访问这些函数,你应该将密码函数移到你的模型中,或者移到库中。实际上,您的模型是一个库。只需将它移动到库中并作为库自动加载,您将以与新代码相同的方式调用它。它仍然不起作用吗?
    function login()
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[12]|xss_clean');
    
        if ($this->form_validation->run() == FALSE)
        {
            $this->index();
        }
        else
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $generated_password = $this->getfunc->reGenPassHash($password);
    
            $user_id = $this->loggedin->check_login($username, $password);
    
            if(! $user_id)
            {
               redirect('/'); 
            }
            else
            {
                $this->session->set_userdata(array(
                    'logged_in' => TRUE,
                    'user_id' => $user_id
                ));
                redirect('cpanel/index');
            }
        }
    }
    
    public function check_login($username, $password)
    {
    $query = "SELECT * WHERE username = ".$username."";
    $result = $this->db->query($query);
    
    if ($result->num_rows == 1)
    {
        $passwordDB = $result->row(0)->password;
        $passwordDB2 = $result->row(0)->password2;
    
    
        return $result->row(0)->user_id;
    
    }
    else
    {
        return false;
    }
    
    }
    
    $this->your_model_with_pass_function->reGenPassHash()