From ac5bc10376b979d812eb3593aab1fa77954867e0 Mon Sep 17 00:00:00 2001 From: jostheta Date: Tue, 23 Jul 2024 11:41:42 +0530 Subject: [PATCH] fixed logiin redirect --- application/config/routes.php | 3 +++ application/controllers/Users.php | 34 ++++++++++++++++++------------- application/views/users/login.php | 2 +- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/application/config/routes.php b/application/config/routes.php index 543b883..fdefddd 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -2,6 +2,8 @@ defined('BASEPATH') OR exit('No direct script access allowed'); //Routes of the forms controller +$route['login_redirect/(:num)'] = 'Users/login_redirect/$1'; + $route['create'] = 'Forms/create'; $route['my_forms'] = 'Forms/my_forms'; $route['my_drafts'] = 'Forms/my_drafts'; @@ -15,6 +17,7 @@ $route['responses/index/(:num)'] = 'Responses/index/$1'; + //Routes of the pages controller $route['hero'] = 'Pages/hero'; $route['default_controller'] = 'Pages/view'; diff --git a/application/controllers/Users.php b/application/controllers/Users.php index 764ac9d..b991f78 100644 --- a/application/controllers/Users.php +++ b/application/controllers/Users.php @@ -28,46 +28,51 @@ } // Log in user - public function login(){ + public function login($form_id = null) { $data['title'] = 'Sign In'; - + $data['form_id'] = $form_id; + $this->form_validation->set_rules('username', 'Username', 'required'); $this->form_validation->set_rules('password', 'Password', 'required'); - - if($this->form_validation->run() === FALSE){ + + if ($this->form_validation->run() === FALSE) { $this->load->view('templates/header'); $this->load->view('users/login', $data); $this->load->view('templates/footer'); } else { - // Get username $username = $this->input->post('username'); // Get and encrypt the password $password = md5($this->input->post('password')); - + // Login user $user_id = $this->user_model->login($username, $password); - - if($user_id){ + + if ($user_id) { // Create session $user_data = array( 'user_id' => $user_id, 'username' => $username, 'logged_in' => true ); - + $this->session->set_userdata($user_data); - + // Set message $this->session->set_flashdata('user_loggedin', 'You are now logged in'); - - redirect('pages'); + + // Redirect to the form response page if form ID is provided + if ($form_id) { + redirect('forms/respond/' . $form_id); + } else { + redirect('pages'); + } } else { // Set message $this->session->set_flashdata('login_failed', 'Login is invalid'); - + redirect('users/login'); - } + } } } @@ -108,6 +113,7 @@ // Log in user with redirection public function login_redirect($form_id = null) { $data['title'] = 'Sign In'; + $data['form_id'] = $form_id; $this->form_validation->set_rules('username', 'Username', 'required'); $this->form_validation->set_rules('password', 'Password', 'required'); diff --git a/application/views/users/login.php b/application/views/users/login.php index 12ff884..7cdc20e 100644 --- a/application/views/users/login.php +++ b/application/views/users/login.php @@ -1,4 +1,4 @@ - +