To retrieve the forgot password - php

In the below code i have used codeigniter code i enter username and password to db.but i want to retrieve my forgot password .I tried but i got the following errors pls help to recify the issue.
Controller:
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site1/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
$this->load->helper('date');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
public function forgot_password()
{
$this->form_validation->set_rules('email_address','Email Address','trim|required|valid_email|callback__check_email_exists');
if($this->form_validation->run() == FALSE)
{
// VALIDATION ERRORS, SHOW VIEWS
$this->index();
}
else
{
// ALL IS GOOD, UPDATE EMAIL, AND REDIRECT TO CURRENT URL
$this->load->view('login_form');
}
}
public function _check_email_exists($email)
{
// LOAD AND USE YOUR MODEL TO CHECK EMAIL EXISTS HERE
if ( ! $email_exists )
{
$this->form_validation->set_message('email_address', 'That email address don\'t exist, sucka!');
return FALSE;
}
else
{
return TRUE;
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
model
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'date' => date('Y-m-d H:i:s', now()),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
if ($this->db->_error_number() == 1062)
{
echo"<script>alert('This value already exists');</script>";
}
if ($this->db->_error_number() == "")
{
$this->session->set_flashdata('create', 'create');
}
return $insert;
}
public function val_forgot_password()
{
$this->db->where('email_address', $this->input->post('email_address'));
$query = $this->db->get($this->members);
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
function curdate() {
// gets current timestamp
date_default_timezone_set('Asia/Manila'); // What timezone you want to be the default value for your current date.
return date('Y-m-d H:i:s');
}
}
view:login_form
<?php $this->load->view('includes/header'); ?>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style1.css" />
<div id="login_form">
<h1>Login!</h1>
<?php
echo form_open('login/validate_credentials');
echo form_input('username', 'Username');
echo form_password('password', 'Password');
echo form_submit('submit', 'Login');
echo anchor('login/signup', 'Create Account');
echo form_close();
?>
</div><!-- end login_form-->
<?php $this->load->view('includes/footer'); ?>
<?php
echo form_open('login/forgot_password');
echo "Email Address: " . form_input('email_address', set_value('email_address', ''));
echo br(2);
echo form_submit('submit','Send Email');
echo form_close();
echo br(1);
echo validation_errors('<p>Error: ');
?>

Well first of all you will need a function in the controller to bring the user to a forgot user page.
public function forgot_pass(){
$this->load->view('forgot_pass_view');
}
Then you will probably need to ask atleast for the email address in the view.
Create another function to verify the email address in the controller, and if it matches send a mail with a new temporary password.
public function check_email(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email_check', 'callback_email_check');
if ($this->form_validation->run() == FALSE){
//throw error
} else {
//send email
}
}
public function email_check(){
//use this function to check if the email exists and return false if it is not the DB
}

Related

User login status activation check in codeigniter?

I'm new in Codeigniter. I have a login system where user with status is 0 can't do login yet and where user with status 1 can login. I might be have a mistake in my code. So, i hope you can find where is my mistake and help me to make it right. here's my code.
My Controller
public function login() {
$this->form_validation->set_rules('no', 'No', 'required|min_length[10]|max_length[16]|integer');
$this->form_validation->set_rules('password', 'password', 'required|md5|xss_clean');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if($this->form_validation->run()== FALSE) {
$this->load->view('v_login');
}else{
$no = $this->input->post('no');
$password = $this->input->post('password');
$cek = $this->m_user->ambilPengguna($no, $password);
$status = $this->m_user->ambilStatus($no); //HERE'S THE PROBLEM
if($cek->num_rows()<> 0 && $status == '1') { //HERE'S TOO, IT WON'T CHECK THE STATUS.
$this->session->set_userdata('isLogin', TRUE);
$this->session->set_userdata('data_user',$cek->row());
redirect('c_belajar');
}else {
echo " <script>
alert('Login failed! call the administrator to activate your account');
history.go(-1);
</script>";
}
}
}
My Model
public function ambilPengguna($no, $password) {
$this->db->select('*');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$this->db->where('password', $password);
$query = $this->db->get();
return $query;
}
public function ambilStatus($no){
$this->db->select('status');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$query = $this->db->get();
return $query;
}
There's a mistake on controller. Please help me.
Alright! May I suggest a little code restructure
Controller
class YourController extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login_model');
$this->load->library('form_validation');
}
public function login()
{
if($_POST)
{
$config=array(
array(
'field' => 'no',
'label' => 'Number',
'rules' => 'trim|required',
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required',
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run()==false)
{
$data['errors']=validation_errors();
$this->load->view('login',$data);
}
else
{
$check=$this->login_model->checkUser($_POST); // you can use xss clean here filter post data
if(!$check)
{
$data['errors']='Invalid Password';
$this->load->view('login',$data);
}
elseif($check==1)
{
$data['errors']='Your account status is not active yet, Please contact Administrator';
$this->load->view('login',$data);
}
else
{
$this->session->set_uerdata($check);
redirect(base_url().'dashboard');
}
}
}
else
{
$this->load->view('login');
}
}
}
Model
class login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function checkUser($data)
{
$st=$this->db->select('*')
->from('tbl_user')
->Where('no_id', $data['no'])
->where('password', $data['password'])
->get()->result_array();// you can use row()
if(count($st)>0)
{
if($st[0]['status']==0){
return 1;
}
else
{
return $st[0];
}
}
else
{
return false;
}
}
}
Try this, this working in my project..
Your Model
public function ambilStatus(){
$this->db->where('no_id', $this->input->post('your input name'));
$query = $this->db->get($this->db->dbprefix . 'tb_user');
$ret = $query->row();
return $ret->account_status;
}
And Your Controller
$status = $this->m_user->ambilStatus();
if($status && $cek->num_rows() == 1 ) {

To change password in mysql

In the below codeigniter code i have placed controller and model i can login by entering username and password.Now i have to implement is to change password.Pls anyone help me to solve the issue.
Controller:
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site1/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
$this->load->helper('date');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
?>
model:
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'date' => date('Y-m-d H:i:s', now()),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
if ($this->db->_error_number() == 1062)
{
echo"<script>alert('This value already exists');</script>";
}
if ($this->db->_error_number() == "")
{
$this->session->set_flashdata('create', 'create');
}
return $insert;
}
function curdate() {
// gets current timestamp
date_default_timezone_set('Asia/Manila'); // What timezone you want to be the default value for your current date.
return date('Y-m-d H:i:s');
}
}
You can follow these simple steps:
Create a view with the change password form (i.e. email, current password and new password)
Create in your controller a function that retrieve the data from the form and check if exists user with that email and that password
Create a function in your model to make the update with $this->db->update()
From your controller run the model function

Login authentication

I have placed 2 controller login and site1.When i login and enter to members area to view pages and if i copy that url and placed in another tab.it opens.But my aim is to display u have no permission to access the page.
Controller:login
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function inactive()
{
echo"<script>alert('In active user Please contact the administrator');</script>";
$this->load->view('login_form');
}
function invalid()
{
echo"<script>alert('Invalid username or password');</script>";
$this->load->view('login_form');
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
if($query->num_rows()>0){
$status = $query->row()->account_status;}
else {
$status = ''; }
//Account active
if($status == 'active')
{
$this->session->set_userdata($data);
redirect('site1/members_area');
}
else if ($status == 'inactive')//Account In active
{ $this->inactive();
}
else // incorrect username or password
{
$this->invalid();
}
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('college_name', 'college_name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
$this->load->helper('date');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
site1:
<?php
class Site1 extends CI_Controller
{
function members_area()
{
$this->load->view('index');
}
function another_page() // just for sample
{
echo 'good. you\'re logged in.';
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
//$this->load->view('login_form');
}
}
}
}
Sessions in PHP are carried over between tabs. The server doesn't really know how to seperate between two tabs. Read the PHP docs on sessions for more information http://us2.php.net/manual/en/intro.session.php

Problems with login at codeigniter [duplicate]

This question already has answers here:
CodeIgniter - Model loaded but can't be used?
(4 answers)
Closed 9 years ago.
thanks for reading my post, i have this issue with a nettus tutorial and codeigniter, i am working with login forms controllers and model, and i made a change to keep level or rol when a user logins, here the error
And here is my controller (login.php)
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) //Existe el usuario
{
$rol = $this->membership_model->roles($this->input->post('username')); //Busco el rol en el modelo
$data = array(
'username' => $this->input->post('username'),
'rol' => $rol,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
//field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_lenght[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_lenght[4]|max_lenght[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|min_lenght[4]|max_lenght[32]|matches[password]');
if (($this->form_validation->run()) == FALSE)
{
$this->signup();
}
else
{
$this->load->model('membership_model');
if ($query = $this->membership_model->create_member)
{
$data['main_content'] = 'signup_succesful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
}
?>
Here is my Model (memebership_model.php)
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function roles($usuario)
{
$sql = "SELECT roles FROM membership WHERE username = ? LIMIT 1";
$q = $this->db->query($sql, $usuario);
if ($q->num_rows() > 0)
{
foreach($q->result() as $row) {
$data = $row;
}
return $data;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'email_address' => $this->input->post('email_address'),
'roles' => '1'
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
?>
Here is the view that shows that error when i execute it (signup_form.php)
<h1>Create an Account</h1>
<fieldset>
<legend>Personal Information</legend>
<?php
echo form_open('login/create_member');
echo form_input('first_name', set_value('first_name', 'First Name'));
echo form_input('last_name', set_value('last_name', 'Last Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
?>
</fieldset>
<fieldset>
<legend>Login Info</legend>
<?php
echo form_input('username', set_value('username', 'Username'));
echo form_input('password', set_value('password', 'Password'));
echo form_input('password2', set_value('password2', 'Password Confirm'));
echo form_submit('submit','Create Account');
?>
<?php echo validation_errors("<p class='error'>"); ?>
</fieldset>
CI_Controller need to call.
class Membership_model extends CI_Model {
Add after
function __construct(){
parent::__construct();
}

Fatal error: Call to a member function where() on a non-object in

I've been debugging this code but still not successful. Can anyone help me out please?
class Membership_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
I kept on getting an fatal error on the line
$this->db->where('username',$this->input->post('username'));
this is the controller/login.php
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
The database probably is not being initialized properly before validate is called.
Looks like you're not connected to your DB. Make sure you are connecting to your database.
You can either connect automatically everytime your script runs or connect to the DB manually. Look at the CI guides for your connection options : http://codeigniter.com/user_guide/database/connecting.html
Could it be that the load->model( 'membership_model' ) should receive the name of a class, and that that name is case sensitive? You should probably check the return value of the API functions...

Categories