.


.


How to change password in CodeIgniter framework


In this example we are going to show you How to change password in CodeIgniter framework PHP.

Here we using 3 files for insert data in MySQL:

  • Forms.php Path: codeIgniter\application\controllers\Forms.php
  • Form_model.php Path: codeIgniter\application\models\Form_model.php
  • change_pass.php Path: codeIgniter\application\views\change_pass.php

Forms.php (Controller)

<?php 
class Forms extends CI_Controller 
{
				public function __construct()
				{
					/*call CodeIgniter's default Constructor*/
					parent::__construct();
					/*load database libray manually*/
					$this->load->database();
					$this->load->library('session');
					/*load Model*/
					$this->load->helper('url');
					$this->load->model('Form_model');
				}       
				
				public function change_pass()
				{
					if($this->input->post('change_pass'))
					{
						$old_pass=$this->input->post('old_pass');
						$new_pass=$this->input->post('new_pass');
						$confirm_pass=$this->input->post('confirm_pass');
						$session_id=$this->session->userdata('id');
						$que=$this->db->query("select * from user_login where id='$session_id'");
						$row=$que->row();
						if((!strcmp($old_pass, $pass))&& (!strcmp($new_pass, $confirm_pass))){
							$this->Form_model->change_pass($session_id,$new_pass);
							echo "Password changed successfully !";
							}
						    else{
								echo "Invalid";
							}
					}
					$this->load->view('change_pass');	
				}
}
?>

Form_model.php (Model)

<?php
class Form_model extends CI_Model 
{
	function fetch_pass($session_id)
	{
	$fetch_pass=$this->db->query("select * from user_login where id='$session_id'");
	$res=$fetch_pass->result();
	}
	function change_pass($session_id,$new_pass)
	{
	$update_pass=$this->db->query("UPDATE user_login set pass='$new_pass'  where id='$session_id'");
	}
}

change_pass.php (View)

<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>

<div id="main">
<div id="login">
<?php echo @$error; ?>
<h2>Change Password</h2>
<br>
<form method="post" action=''>
		<label>Old Password :</label>
		<input type="password" name="old_pass" id="name" placeholder="Old Pass"/><br /><br />
		<label>New Password :</label>
		<input type="password" name="new_pass" id="password" placeholder="New Password"/><br/><br />

		<label>Confirm Password :</label>
		<input type="password" name="confirm_pass" id="password" placeholder="Confirm Password"/><br/><br />
		<input type="submit" value="login" name="change_pass"/><br />
</form>
</div>
</div>
</body>
</html>

Run the program on your browser with URL:

http://localhost/codeIgniter/index.php/Forms/change_pass

Here codeIgniter is my folder name. Put your folder name instead of codeIgniter.Rest of things are same.