title: How to md5 a password on registration in codeigniter
body: <p>I have a user registration form created using codeigniter which works fine if I am not using md5 but the problem is whenever I try to md5 the password, the validation fails. I have googled for possible solutions but couldn't find any. Here are the code for my controller and model. </p>
  
  <p>How can I pass an md5 value to the $userdata array ? </p>
  
  <pre><code>    &lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Register extends CI_Controller {
  
      /**
       * Index Page for this controller.
       *
       * Maps to the following URL
       *      http://example.com/index.php/welcome
       *  - or -  
       *      http://example.com/index.php/welcome/index
       *  - or -
       * Since this controller is set as the default controller in 
       * config/routes.php, it's displayed at http://example.com/
       *
       * So any other public methods not prefixed with an underscore will
       * map to /index.php/welcome/&lt;method_name&gt;
       * @see http://codeigniter.com/user_guide/general/urls.html
       */
      function __construct()
      {
         parent::__construct();
         $this-&gt;load-&gt;model('registration','',TRUE);  
       }
  
  
  
      function index()
      {
          $this-&gt;load-&gt;helper(array('form', 'url'));
          $this-&gt;load-&gt;library('form_validation');
          $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
          $this-&gt;form_validation-&gt;set_rules('email', 'Email Address', 'required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('password1', 'Confirm Password', 'required|xss_clean|matches[password]|md5');
          $this-&gt;form_validation-&gt;set_rules('lastname', 'Last Name', 'trim|required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('firstname','First Name', 'trim|required|xss_clean');
          /*$this-&gt;form_validation-&gt;set_rules('primarycontact', 'Primary Contact', 'trim|required|xss_clean');*/
          $this-&gt;form_validation-&gt;set_rules('companyname' , 'Company Name', 'required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('address', 'Address', 'required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('country', 'Country', 'required');
          $this-&gt;form_validation-&gt;set_rules('telephone', 'Primary Telephone', 'required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('mobile', 'Mobile No', 'xss_clean');
          $this-&gt;form_validation-&gt;set_rules('businesstype', 'Business Type', 'required');
          $this-&gt;form_validation-&gt;set_rules('provider', 'Provider Name', 'required');
          $this-&gt;form_validation-&gt;set_rules('type', 'Business Type', 'trim|required|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('grouping', 'Power Grouping', 'required|callback_set_user_data');     
          if ($this-&gt;form_validation-&gt;run() == TRUE)
          {
  
              $this-&gt;load-&gt;view('regsuccess');
  
          }
          else
          {
              $this-&gt;load-&gt;view('register');
  
  
          }
      }
  
      function hashpassword() {
          return md5($this-&gt;input-&gt;post('password1'));
      }
  
  
      function set_user_data()
       {
  
          $userdata = array(
                          'username'  =&gt; $this-&gt;input-&gt;post('username'),
                          'password'  =&gt; $this-&gt;input-&gt;post('password1'),
                          'email'  =&gt; $this-&gt;input-&gt;post('email'),
                          'firstname'  =&gt; $this-&gt;input-&gt;post('firstname'),
                          'lastname'  =&gt; $this-&gt;input-&gt;post('lastname'),
                          /*'primarycontact'  =&gt; $this-&gt;input-&gt;post('primarycontact'),*/
                          'companyname'  =&gt; $this-&gt;input-&gt;post('companyname'),
                          'address'  =&gt; $this-&gt;input-&gt;post('address'),
                          'country'  =&gt; $this-&gt;input-&gt;post('country'),
                          'telephone'  =&gt; $this-&gt;input-&gt;post('telephone'),
                          'mobile'  =&gt; $this-&gt;input-&gt;post('mobile'),
                          'businesstype'  =&gt; $this-&gt;input-&gt;post('businesstype'),
                          'type'  =&gt; $this-&gt;input-&gt;post('type'),
                          'powergrouping'  =&gt; $this-&gt;input-&gt;post('grouping')              
                          );
  
          $this-&gt;registration-&gt;index($userdata);
  
  
       }
  
       function check_username($username){
  
              $res = $this-&gt;registration-&gt;check_username($username);
              if($res == TRUE) {
  
                  return TRUE;
              }
              else {
                  return FALSE;
              }
       }
  
  
  }
  </code></pre>
  
  <p>MY Model 
   
  
  <pre><code>    function index($userdata) {
              //var_dump($userdata);
          $this-&gt;db-&gt;insert('userdetails', $userdata); 
          $this-&gt;load-&gt;library('email');
          $this-&gt;email-&gt;from('info@thermalimage.ie', 'Thermalimage Ireland');
          $this-&gt;email-&gt;to($userdata['email']);
          $this-&gt;email-&gt;subject('Registration Successful');
          $this-&gt;email-&gt;message('Your registration has been successful.');     
          $this-&gt;email-&gt;send();
          /*echo $this-&gt;email-&gt;print_debugger();*/
  
          return TRUE;
      }
  
      function check_username($username) {
          $user = $username;                      
          $this-&gt;db-&gt;select('username');
          $this-&gt;db-&gt;where('username', $user);
          $this-&gt;db-&gt;from('userdetails');
          $query = $this-&gt;db-&gt;get();
          $thisresult = $query-&gt;num_rows();
          if(  $thisresult&gt; 0 ){
              return FALSE;
          }else{
  
              return TRUE;
          }
       } 
  </code></pre>
  
  <p>}</p>