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
Related
I am trying to develop a login panel using codeigniter but I am unable to do so as I believe my concept is not so clear yet though or Am i doing something wrong please help me out with this concern
Controllers>admin.php
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run()) {
$this->verify_user->can_log_in();
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_id') == true) {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
models>verify_users.php
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
$this->load->view('admin/login', $data);
}
}
}
The thing is happening when I login with correct username and password it redirects me back to login.php when I put the model script within the verify function it runs perfectly
Please help me out with this
This is the closest possible fix to your way of implementation.
You need to consider reading more about MVC.
Try replace your controller with this:
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run() && $this->verify_user->can_log_in()) {
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_in') == "1") {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
}
And your model with this:
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => "1"
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
return false;
}
}
}
Check this
class admin extends CI_Controller {
^// this should be Admin
IN model
else {
//$data['message'] = 'Incorrect username/password';
//$this->load->view('admin/login', $data);
//dont load view in model
return false;
}
In controller
if($this->form_validation->run()) {
$res = $this->verify_user->can_log_in();
if($res)
redirect('admin/dashboard');
else
redirect('admin/login');
} else {
$this->load->view('admin/login');
}
Fixing these 3 errors should help you.
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
}
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
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();
}
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...