0

I'm trying to validate my user server side with php and its saying I'm getting an Fatal error: Call to undefined function reGenPassHash() in /home/xtremer/public_html/kowmanager/application/models/loggedin.php on line 13 now I have the model that includes the reGenPassHash function auto loaded so I thought it'd be available to use but for some reason its not because of this message. Someone explain why?

Model:

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

}

Controller:

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

EDIT :

I'm trying to make sure my logic is correct. Should I be working with the regenPassHash function call in my controller instead?

EDIT 2 :

This is an example of how my password functions look (getfunc model):

<?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

?>

Controller:

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

Model:

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

}
2
  • Can you show us the code of the model where the reGenPassHash() function is? Are you definitely including that file ABOVE the check_login function? Commented Dec 31, 2011 at 18:02
  • If its autoloaded wouldn't that mean that its included above this model. Commented Dec 31, 2011 at 18:04

4 Answers 4

3

reGenPassHash() function is not visible to the check_login() method. call the reGenPassHas with the object.

ex: $object->reGenPassHas().

this is a scope issue

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, Best practice to have reGenPassHas() in controlloer as it is not playing with your database. if your methods going to working with DB, best practice is to move them to the model. Model–view–controller
1

Autoloading is only going to kick in for object methods. You're not using object notation on your call to the regen function, so it's being treated like a regular function call - and this function is not defined.

2 Comments

Can I call a function from anther model inside of this model?
So your saying it should be $generated_password = $this->model->getfunc->reGenPassHash($password); instead of $generated_password = reGenPassHash($password);
1

You need to call your reGenPassHash function through your model object like:

$this->your_model_with_pass_function->reGenPassHash()

1 Comment

Your code should work as you have edited it now, but as other posts said you should move your password functions into your model or else into a library if you need more than one controller to have access to the functions. Really your model is a library. Just move it to libraries and autoload it as a library and you will call it the same way as your new code shows. Is it still not working?
1

The only error i saw with your code, is that you are calling your method the wrong way. For any class method, you need a objects to access the methods.

This might $this. in your case or something else in other cases.

Try using $this -> reGenPassHash() to call the method only if it relies with in the model, or you will need respective object modifier.

UPDATE:

  1. Include your GenPassHash() & reGenPassHash() on your controller.
  2. Instead of $this->getfunc->reGenPassHash() use $this->reGenPassHash()

2 Comments

Yes, logically, your model should contain your database related logic only. .... The function reGenPassHash() is related on developing a hashed code only, independent of the database... so the code should be placed in controller instead :)
I think I'm still stuck on this because how I should be achieving my goal. I'm going to update my post again with some updated stuff and see if it makes sense to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.