How can i make a reset password function in codeigniter? - php

Whenever i run this code, i recieve a blank screen on submission. The code does not seem to work and i'm unsure why. Can anyone see an issue? I've checked it for syntax already mutliple times.
Controller:
public function changepwd(){
$this->form_validation->set_rules('oldpassword','Old Password','required|trim|xss_clean|callback_change');
$this->form_validation->set_rules('password1','New Password','required|trim');
$this->form_validation->set_rules('password2','Confirm Password','required|trim|matches[password1]');
if($this->form_validation->run()= true){
redirect('admin');
}else{
$this->form_validation->set_message('change','info is wrong');
redirect('admin');
}
}
public function change(){
$this->load->view('error_display');
$oldpass = $this->input->post('oldpassword');
if(isset($oldpass)){
if($this->input->post('password1') == $this->input->post('password2')){
$session=$this->session->userdata("logged_in");
$username= $session['username'];
$query = "SELECT * FROM database where username='$username'";
$result = $this->db->query($query);
$row=$result->row();
$pass = $row->password;
$s= $row->salt;
$user_old_pass = sha1($s.$oldpass);
if($pass == $user_old_pass){
$new = substr(uniqid(rand(), true), 0, 20);
$users_password = $this->input->post('pass1');
$new_password = sha1($new.$user_password);
$data = array($new,$new_password);
$this->update_model->insert_model($data);
$this->form_validation->set_message('change','saved');
$this->load->view('admin');
}else{
$this->form_validation->set_message('change','<p class="error">Incorrect Password</p>');
return false;
redirect('admin');
}
}else{
$this->form_validation->set_message('change','passwords dont match');
return false;
redirect('admin');
};
}else{
$this->form_validation->set_message('change','old password is not entered');
return false;
redirect('admin');
}
model:
function login($data)
{
$this->db->update('database', $data);
}

your
if($this->form_validation->run()= true){
should be
if($this->form_validation->run() == FALSE) {
because you are redirecting every time to 'admin'

Related

Codeigniter, Field not updating but other fields will update

Here is my function
function store(){
$this->load->library('form_validation');
$this->form_validation->set_rules('shorthand','Shorthand','required|is_unique[locations.shorthand]');
if($this->form_validation->run() == FALSE){
redirect('locations');
} else {
$id = $this->input->post('location_id');
$location_name = ucwords(strtolower($this->input->post('location_name')));
$shorthand = strtolower($this->input->post('shorthand'));
$station = ucwords(strtolower($this->input->post('station')));
$data = array(
'location_name'=>$location_name,
'shorthand'=>$shorthand,
'station'=>$station
);
}if($id){
$result = $this->location_model->update($data,$id);
if($result > 0){
$this->session->set_flashdata('success', 'Update successfull!');
}else{
$this->session->set_flashdata('error', 'Failed to update!');
}
redirect('locations');
}else{
$result = $this->location_model->create($data);
if($result > 0){
$this->session->set_flashdata('success', 'Location added!');
}else{
$this->session->set_flashdata('error', 'Failed to add location!');
}
redirect('locations');
}
}
The shorthand updates fine but station and location_name wont update no matter what I do. But if I remove this chunk of code.
$this->load->library('form_validation');
$this->form_validation->set_rules('shorthand','Shorthand','required|is_unique[locations.shorthand]');
if($this->form_validation->run() == FALSE){
redirect('locations');
} else {
All of the fields will update. Can you please look at my codes? Thanks in advance.
Here is my Update Query
function update($data, $id){
$this->db->where('location_id', $id);
$this->db->update('locations', $data);
return $this->db->affected_rows();
}
I think this what you need. Please manage according to your need
$locations = $this->db->get_where('locations', array('location_id' => $id))->row();
$original_value = $locations->shorthand;
if($this->input->post('shorthand') != $original_value) {
$is_unique = '|is_unique[locations.shorthand]';
} else {
$is_unique = '';
}
$this->form_validation->set_rules('shorthand','Shorthand','required|trim|xss_clean'.$is_unique);
Guys for the people who have commented, thank you but I have already solved the problem it
was inside controller I forgot to put else after this line
if($this->form_validation->run() == FALSE){
redirect('locations');
}
This is how the whole function looks now
function store(){
$this->form_validation->set_rules('location_name','Location_name','required');
$this->form_validation->set_rules('shorthand','Shorthand','required|callback_locationExists');
$this->form_validation->set_rules('station','Station','required');
$id = $this->input->post('location_id');
$location_name = ucwords(strtolower($this->input->post('location_name')));
$shorthand = strtolower($this->input->post('shorthand'));
$station = ucwords(strtolower($this->input->post('station')));
$data = array(
'location_name'=>$location_name,
'shorthand'=>$shorthand,
'station'=>$station,
);
if($this->form_validation->run() == FALSE){
redirect('locations');
}else{
if($id){
$result = $this->location_model->update($data,$id);
if($result > 0){
$this->session->set_flashdata('success', 'Update successfull!');
}else{
$this->session->set_flashdata('error', 'Failed to update!');
}
redirect('locations');
}else{
$result = $this->location_model->create($data);
if($result > 0){
$this->session->set_flashdata('success', 'Location added!');
}else{
$this->session->set_flashdata('error', 'Failed to add location!');
}
redirect('locations');
}
}
}
Thanks for reading

passing user information to view in codeigniter

I am trying to get users details from the database and put it in session so that it can be used in the view. I have tried all I can but I keep getting error. Undefined Variable Email.
MODEL:
function login($username, $password)
{
$this->db->select('authTbl.id, authTbl.password, authTbl.username, authTbl.email, authTbl.mobile');
$this->db->from('users as authTbl');
$this->db->where('authTbl.username', $username);
$this->db->where('authTbl.isDeleted', 0);
$query = $this->db->get();
$user = $query->result();
if(!empty($user)){
if(verifyHashedPassword($password, $user[0]->password)){
return $user;
} else {
return array();
}
} else {
return array();
}
}
CONTROLLER:
function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
$data['title'] = 'Login';
if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
{
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
else
{
redirect('posts');
}
}
/**
* This function used to logged in user
*/
public function login()
{
$this->load->library('form_validation');
$data['title'] = 'Login';
$this->form_validation->set_rules('username', 'Username', 'required|max_length[128]|trim');
//$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]|');
if($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
else
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->user_model->login($username, $password);
if(count($result) > 0)
{
foreach ($result as $res)
{
$sessionArray = array('id'=>$res->id,
'username'=>$res->username,
'email'=>$res->email,
'mobile'=>$res->mobile,
'isLoggedIn' => TRUE
);
$this->session->set_userdata($sessionArray);
$this->session->set_flashdata('user_login', 'Welcome');
redirect('posts');
}
}
else
{
$this->session->set_flashdata('login_fail', 'Email or password mismatch');
redirect('users/login');
}
}
}
VIEW:
<?php echo $email; ?>
You are creating the session for user information
Try to fetch the session data :
echo $this->session->userdata('email');
or trying to pass the data in views $data
$email will not work because writing $email means ordinary variable but in your case you have to write like :
<?php echo $this->session->userdata('email'); ?>
If You are using flashdata, then You can get it using below code:
Controller
if (empty($this->session->userdata('UserID'))) {
$this->session->set_flashdata('flash_data', 'You don\'t have access!');
$this->load->view("initialHeader");
$this->load->view("login");
redirect('Login');
}
View
<?php if(!empty($this->session->flashdata('flash_data'))) {
?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('flash_data'); ?>
</div>
<?php } ?>
If You are using tmp data as a session, please refer below code:
Controller
<?php
if (!empty($data)) {
$this->session->set_userdata('permission_error_msg', $data);
$this->session->mark_as_temp('permission_error_msg', 10); // For 10 Seconds
$this->load->view('dashboard', array($title, $data));
} ?>
View
<?php if ($this->session->tempdata('permission_error_msg')) { ?>
<b class="login_error_msg"><?php
if (!empty($this->session->tempdata('permission_error_msg'))) {
echo $this->session->tempdata('permission_error_msg');
}
?></b>
<?php } ?>
Thank You.

change password on codeigniter

I tried to make a change password function, but nothing happened.I actually adapted codeigniter change password code but again, nothing happened.Here I attached the code, any hep would be appreciated.
the controller -
function change_password_process()
{
$this->load->view('ubahpassword_view');
$this->load->library('form_validation');
$this->load->library('session');
$this->form_validation->set_rules('pass_lama','Password Lama','trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('pass_baru','Password Baru','trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('ulangpass_baru','Ulangi Password Baru','trim|required|min_length[4]|max_length[32]|matches[pass_baru]');
if ($this->form_validation->run() == FALSE)
{
redirect('ubahpassword');
}
else
{
$query = $this->rekammedis_model->change_password();
redirect('ubahpassword');
}
}
The model -
function change_password()
{
$this->db->select('id');
$this->db->where('username', $this->session->userdata('username'));
$this->db->where('id', $this->session->userdata('id'));
$this->db->where('password', md5($this->input->post('pass_lama')));
$query = $this->db->get('user');
if ($query->num_rows() > 0)
{
$row = $query->row();
if($row->id === $this->session->userdata('id'))
{
$data = array(
'password' => md5($this->input->post('pass_lama'))
);
$this->db->where('username', $this->session->userdata('username'));
$this->db->where('password', md5($this->input->post('pass_lama')));
if($this->db->update('user', $data))
{
return "Password berhasil diganti!";
}
else
{
return "Terdapat kesalahan, password tidak terganti";
}
}
else
{
return "Terdapat kesalahan, password tidak terganti";
}
}
else
{
return "Password lama salah";
}
}
Assuming baru means new and lama means old.
You were changing your old password with old password itself (pass_baru)
So Replace
$data = array('password' => md5($this->input->post('pass_lama')));
with
$data = array('password' => md5($this->input->post('pass_baru')));
UPDATE
Found Another Bug
if ($this->form_validation->run() == FALSE)
{
redirect('ubahpassword');
}
You can't redirect it, if you do, you won't get validated here.
Load your view here itself. Also update your code above if it does not work

Login section not working well in php codeigniter

I'm using php codeigniter for my project. In my login page if username and password is invalid just load the login page, else load the home. if invalid, First time it loads the login page again given the wrong details for login one controller name is added in url like local turns like localhost/project name/administrator/administrator/login_authentication
my code is
function index()
{
if($this->session->userdata('usertype') != '')
{
redirect('administrator/administrator_view');
}
else
{
$this->load->view('login');
}
}
function login_authentication()
{
$username=$this->input->post('username');
$password=$this->input->post('password');
$user = $this->administrator->admin_authentication($username,$password);
if(count($user) == 1)
{
foreach($user as $admin_value)
{
$user_name=$admin_value['UserName'];
$usertype=$admin_value['UserType'];
}
$session_data = array(
'username' => $user_name,
'usertype' => $usertype,
);
$this->session->set_userdata($session_data);
if($usertype == 1)
{
redirect('administrator/administrator_view');
}
}
else
{
$data['Invalid_Login']="Invalid Username and Password";
$this->load->view('login',$data);
}
}
function administrator_view()
{
if($this->session->userdata('usertype') == '')
{
redirect('administrator');
}
else
{
$data['heading'] = '';
$this->load->view('header', $data);
$this->load->view('dashboard', $data);
$this->load->view('footer');
}
}
Admin authentication function
function admin_authentication($username, $password)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('UserName',$username);
$this->db->where('Password',$password);
$query = $this->db->get();
return $query->result_array();
}
I'm trying more than one time given not correct information for login everytime one controller name added in url. Please help me.
Thanks in advance.
change
$this->session->set_userdata($session_data);
to
$this->session->set_userdata(('some_name', $session_data);
and change
if($this->session->userdata('usertype') == '')
in all area to
$ses = $this->session->userdata('some_name');
if($ses['usertype'] == '')
and try....
first of all check if there is an post request in your function login_authentication() like this:
function login_authentication()
{
if( $this->input->post(null) ){
//your authentication code here
}else{
//load the login view here
}
}
Here is your function:
function login_authentication(){
if( $this->input->post(null) ){ //check if there is an post request
$username=$this->input->post('username');
$password=$this->input->post('password');
$user = $this->administrator->admin_authentication($username,$password);
print_r( $user );die(); //the user array as returned from the model see if its correct or not
if(count($user) == 1)
{
foreach($user as $admin_value)
{
$user_name=$admin_value['UserName'];
$usertype=$admin_value['UserType'];
}
$session_data = array(
'username' => $user_name,
'usertype' => $usertype,
);
print_r( $session_data );die; //see if it builds the correct array or not
//$this->session->set_userdata($session_data);
$this->session->set_userdata('user_info',$session_data); //to read the username use like $this->session->userdata['user_info']['username'];
if($usertype == 1)
{
redirect('administrator/administrator_view');
}
}else{ //invalid credentials load the login view
$this->session->set_flashdata('Invalid_Login', 'Invalid username or password!'); //to echo in view use $this->session->flashdata('Invalid_Login');
redirect('administrator', 'refresh');
}
}else{ //redirect to index function now
redirect('administrator', 'refresh');
}
}
In your function administrator_view(),
function administrator_view(){
if( !$this->session->userdata('user_info') ){
print_r( $this->session->all_userdata() );die('no session set redirecting'); //the session is not set here
redirect('administrator');
}
else{
$data['heading'] = '';
$this->load->view('header', $data);
$this->load->view('dashboard', $data);
$this->load->view('footer');
}
}

setcookie() gives no error, but the cookie does not set

I have a application based on CodeIgniter . I tried to set cookie in on of my controller functions. Here is the part of my code
private function login_core($username, $user_pass){
//Get value from database
$this->load->model('user_model');
if($this->user_model->init($username) == 1){
$pass = $this->user_model->getPassword();
$this->e['db_pass'] = $pass;
//Now encrypt th real password
//Make thisL
$e_p = $username. $user_pass . $this->user_model->getEmail();
$e_p = sha1($e_p);
$this->e['act_pass'] = $e_p;
//Whenever the email is changed, update the password. (Require password to change the email)
$this->load->library('encrypt');
$this->load->library('xsecurity');
//cookie not set :( . And i don't know the reason
if($pass == $e_p){
$this->xsecurity->SetInfoLocal($e_p);
setcookie('y',$this->encrypt->encode($username, $_SERVER['HTTP_USER_AGENT']),60*60*24*3);
return true;
} else {
return false; //false'
}
} else {
return false;
}
}
public function login(){
if(!isset($_POST['user'], $_POST['pass'])){
$this->load->view('header');
$this->load->view('content/errorError');
$this->load->view('footer');
return false;
}
$user = $_POST['user'];
$pass = $_POST['pass'];
if(self::login_core($user, $pass)){
$d['a'] = $this->e;
$this->load->view('header', $d);
if(isset($_GET['redirect_to'])) {
ob_start();
header('location:'. $_GET['redirect_to']);
return true;
}
$data['userloggedOn'] = true;
$this->load->view('main', $data);
}
else
{
$data['userloggedOn'] = false;
$this->load->view('main', $this->e);
}
$this->load->view('footer');
}
The user points to login via a form, and to my thinking, no any headers are sent before setting cookie (for the login page).when i wrap if around setcookie, the result is true, but the cookie does not set? Why?
don't you need to added the current time to the expire date?
for example
setcookie("y", $unique_string, time() + (60*60*24*30), '/');

Categories