Create Change Password functionality in codeigniter along with css framework

hey guys, lets see the change password functionality in codeigniter using css framework called twitter bootstrap 🙂

Steps:

1. Create a view in codeigniter for a form displaying old password,new & confirm password labels…

<?php

echo form_open(‘main/changepwd’);
echo validation_errors();

?>
<table class=”table table-bordered”>

<tbody>

<tr>
<td><small><?php echo “Old Password:”;?></small></td>
<td><?php echo form_password(‘opassword’);?></td>

</tr>
<tr>
<td><small><?php echo “New Password:”;?></small></td>
<td><?php echo form_password(‘npassword’);?></td>

</tr>
<tr>
<td><small><?php echo “Confirm Password:”;?></small></td>
<td><?php echo form_password(‘cpassword’);?></td>

</tr>
</tbody>
</table>
&nbsp;&nbsp;<div id=”some”style=”position:relative;”><button type=”submit” class=”btn btn-primary”><i class=” icon-ok-sign icon-white”></i>&nbsp;Submit</button>

<?php

echo form_close();

?>

2. Add this code in Controller page..

public function changepwd(){
$this->load->library(‘form_validation’);
$this->form_validation->set_rules(‘opassword’,’Old Password’,’required|trim|xss_clean|callback_change’);
$this->form_validation->set_rules(‘npassword’,’New Password’,’required|trim’);
$this->form_validation->set_rules(‘cpassword’,’Confirm Password’,’required|trim|matches[npassword]’);

if($this->form_validation->run()!= true)
{
$this->load->view(‘settings’);

}
}
public function change() // we will load models here to check with database
{
$sql = $this->db->select(“*”)->from(“users”)->where(“email”,$this->session->userdata(’email’))->get();

foreach ($sql->result() as $my_info) {

$db_password = $my_info->password;
$db_id = $my_info->id;

}

if(md5($this->input->post(“opassword”)) == $db_password){

$fixed_pw = mysql_real_escape_string(md5($this->input->post(“npassword”)));
$update = $this->db->query(“Update `users` SET `password`=’$fixed_pw’ WHERE `id`=’$db_id'”)or die(mysql_error());
$this->form_validation->set_message(‘change’,'<div class=”alert alert-success”><a href=”#” class=”close” data-dismiss=”alert”>&times;</a>
<strong>Password Updated!</strong></div>’);
return false;

}else
$this->form_validation->set_message(‘change’,'<div class=”alert alert-error”><a href=”#” class=”close” data-dismiss=”alert”>&times;</a>
<strong>Wrong Old Password!</strong> </div>’);

return false;

}

In 2 steps you easily did change password functionality in code igniter na.. 😉 good luck 🙂

Advertisements

11 thoughts on “Create Change Password functionality in codeigniter along with css framework

  1. What’s up i am kavin, its my first time to commenting
    anywhere, when i read this paragraph i thought i could also
    make comment due to this brilliant post.

  2. It’s great!! I have used your code, It works until I click submit after changing password. getting a Undefined variable: $db_password. Have looked but can’t see where I am going wrong, Please Help!

  3. I have used your code, It works until I click submit after changing password. getting a Undefined variable: $db_password. Why is this error occured?

    1. Hi Janak,
      I am not into codeigniter anymore now..i implemented this years back..versions has been changed now completely. Post in stackoverflow for proper answer. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s