2011-12-31 102 views
0

我試圖驗證我的用戶服務器端與PHP和它說我得到一個致命錯誤:調用未定義的函數reGenPassHash()在/ home/xtremer/public_html/kowmanager/application/models /第13行的loggedin.php現在我有包含reGenPassHash函數自動加載的模型,所以我認爲它可以使用,但由於某種原因它不是因爲這個消息。有人解釋爲什麼?驗證與php

型號:

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 */ 

編輯:

我想確保我的邏輯是正確的。我應該在我的控制器中使用regenPassHash函數調用嗎?

編輯2:

這是我的密碼功能如何看待(getfunc模型)的例子:

<?php 
function GenPassHash($logPass) 
{ 
    $usersalt = substr(md5(uniqid(rand(), true)), 0, 11); 
    $encPass = sha1($logPass); 
    $sltPass = $encPass . $usersalt;$encSPass = sha1($sltPass); 
    $passArray = array($encSPass,$usersalt); 
    return $passArray; 
} 
function reGenPassHash($postDpass, $storeSalt) 
{ 
    $logPass = $postDpass; 
    $encPass = sha1($logPass); 
    $sltPass = $encPass . $storeSalt; 
    $encSPass = sha1($sltPass); 
    return $encSPass; 
} 

//useage 
$logPass = "catcher05";//this could be your posted variable from registration 

$passforDB = GenPassHash($logPass); 
echo "<pre>"; 
print_r($passforDB); 
echo "</pre>"; 
echo "Encrypted Password: " . $passforDB[0] . "<br />"; 
echo "Salted Value: " . $passforDB[1] . "<br />"; 

echo "----------------------------------<br />"; 
//in this example I post $passforDB[1] with the below function to stimulate having pulled it from a DB 
echo reGenPassHash($logPass, $passforDB[1]); //you would query based on your username being posted and only pull the salt and encrypted pass you would use the salt in this function 

?> 

控制器:

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; 
} 

} 
+0

你能告訴我們reGenPassHash()函數的模型代碼嗎?你確定在check_login函數中包含該文件嗎? – 2011-12-31 18:02:52

+0

如果它的自動加載不意味着它包含在這個模型之上。 – 2011-12-31 18:04:05

回答

1

自動加載只能用於對象方法。您在調用regen函數時沒有使用對象表示法,因此它被當作常規函數調用來處理 - 並且此函數未定義。

+0

我可以從這個模型中的另一個模型調用一個函數嗎? – 2011-12-31 18:05:15

+0

所以你說它應該是$ generated_pa​​ssword = $ this-> model-> getfunc-> reGenPassHash($ password);而不是$ generated_pa​​ssword = reGenPassHash($ password); – 2011-12-31 18:06:47

+0

請查看我的帖子編輯。 – 2011-12-31 18:13:19

1

你需要通過你的模型對象調用你的reGenPassHash功能,如:

$this->your_model_with_pass_function->reGenPassHash() 
+0

請查看我的帖子編輯。 – 2011-12-31 18:14:02

+0

您的代碼應該像您現在編輯它一樣工作,但正如其他帖子所說,如果您需要多個控制器才能訪問這些功能,則應將密碼功能移動到您的模型或其他庫中。真的,你的模型是一個圖書館。只要將它移動到庫中,並將其自動加載爲庫,並且將按照新代碼顯示的方式調用它。它還沒有工作嗎? – davidethell 2012-01-01 03:13:45

1

我與您的代碼看到的唯一錯誤,是你在呼喚你的方法錯誤的方式。對於任何類方法,您需要一個對象來訪問這些方法。

這可能是$ this。在你的情況下或其他情況下的其他情況。

嘗試使用$this -> reGenPassHash()只有在模型中使用方法或者您需要各自的對象修飾符。

UPDATE:

  1. 包括您的控制器上GenPassHash()& reGenPassHash()。
  2. 代替$this->getfunc->reGenPassHash()使用$this->reGenPassHash()
+0

請查看我的帖子編輯。 – 2011-12-31 18:13:42

+1

是的,邏輯上,您的模型只應包含與數據庫相關的邏輯。 ....函數reGenPassHash()僅與開發散列代碼有關,與數據庫無關...所以代碼應該放置在控制器中:) – Starx 2011-12-31 18:17:49

+0

謝謝您的澄清。 – 2011-12-31 18:28:54

3

reGenPassHash()函數是不將check_login()方法可見。用對象調用reGenPassHas。

ex:$object->reGenPassHas()

這是一個範圍問題

+0

請檢查我的文章編輯。 – 2011-12-31 18:14:20

+1

是的,最好的做法是在控制器中使用'reGenPassHas()',因爲它沒有與數據庫一起玩。如果你的方法與數據庫一起工作,最好的做法是將它們移動到模型中。 [Model-view-controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) – 2011-12-31 18:39:31

+0

再次更新我的文章。 – 2011-12-31 18:47:07