Dashboard Failes to load after sign in - codeigniter

After right login details,form redirects to same place
This is my controller for the login
The login should be redirected to the dashboard after validation and database check
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
if($this->session->userdata('logged_in')) {
redirect(base_url().'dashboard');
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
redirect(base_url().'dashboard');
}
}
$this->load->view('Templates/header', $template);
$this->load->view('Login/login_form');
$this->load->view('Templates/footer');
}
}
code for Dashboard
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('dashboard_model');
if(!$this->session->userdata('logged_in')) {
redirect(base_url());
}
}

Try This Method
redirect('dashboard');

Try site_url() as well. Make sure there are proper code(error free) of view in dashboard controller.
redirect(site_url()."/dashboard");

Related

Redirect is not working in Codeigniter on real server. Error reporting in on but not showing any error

I have Codeigniter 2 installed on my server. redirect is not working on the login page. I can provide the URL.
This is my controller file of login. I have set the error reporting on but not displaying any error. Kindly help me with this. The script is taken from truebus.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_admin extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('AdminLogin_model');
$this->load->model('Settings_model');
$this->load->library('session');
if($this->session->userdata('logged_in_admin')) {
redirect(site_url('bus_details/view_busdetails'));
}
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
// redirect(base_url().'Bus_details/view_busdetails');
redirect(site_url('bus_details/view_busdetails'));
}
}
$this->load->view('login-form');
}
I think you didn't include URL helper
$this->load->helper('url');
you should include in controller
$this->load->helper('url);
redirect()

Codeigniter User Agent Not Working On Logout Controller

In my MY_Controller.php I have a redirect in there if session has expired.
Then it redirects to the logout controller. I have added a if statement agent->is_referral to the logout controller and set flashdata. Because it has been redirect from dashboard controller it should pick up that if statement and set flashdata but does not.
Question: If the customer is redirected from whatever controller to logout then how to get is_referral() to detect and set the flashdata message. flashdata message is not setting when inside is_referral() if statement.
controllers > account > Logout.php
<?php
class Logout extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->library('user_agent');
if ($this->agent->is_referral()) {
// Does not set the flashdata if redirected from another controller.
$this->session->set_flashdata('warning', 'Your session has expired!');
}
// Flashdata works if have it here.
// $this->session->set_flashdata('warning', 'Your session has expired!');
$this->customer->logout();
redirect('/'); // redirect to login page.
}
}
core > MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if ($this->uri->segment(1)) {
if (!$this->session->userdata('customer_id')) {
$route = $this->uri->segment(1) .'/'. $this->uri->segment(2);
if (isset($route)) {
$ignore = array(
'account/logout'
);
if (!in_array($route, $ignore)) {
redirect('account/logout');
}
}
}
}
}
}
controllers > account > Dashboard.php
<?php
class Dashboard extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('common/header_view');
$this->load->view('account/dashboard_view');
$this->load->view('common/footer_view');
}
}

Code Igniter session declaration

I am new to CodeIgniter framework. I am using 2.1.4 version. I designed a simple login form, with a javascript validation, and the home page of a site. Can you please help me to understand how to declare session , and how to destroy the session on clicking signout link.
controller file of login page ( to load the view page login.php ):-
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index(){
$this->load->view('login');
}
function success() {
redirect ('home');
}
}
The controller file home.php for the view home.php
class Home extends CI_Controller {
// local constructor will be overriding the one in the parent controller class
// for using a constructor in any of my Controllers
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
}
I have designed the view page home.php, and gave the signout link:-
<div class="logout">Signout</div>
For initializing the session, i need to know, what all constructor changes/ config changes need, and the method of session destoy.
To start session library, Go to application/config/config.php and change the below line:
$autoload['libraries'] = array('session');
It would be better if you start your session in the autoload.php. To destroy session you would use :
$this->session->sess_destroy();
To set session :
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
here is an controller... first of all u need to declare a session so that you have two choice to declare one is Go to application/config/config.php change the code as
$autoload['libraries'] = array('session');
and follow this following method (controller)
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
}
function index(){
$this->load->view('login');
}
function success() {
$user=$this->input->post('user');
$psw=$this->input->post('pswd');
$this->load->model('validation');
$result=$this->validation->useraccess($user,$psw);
if($result)
{
$this->session->set_userdata('username', $user); //setting session
redirect ('home');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect('login','refresh');
}
}
this is model where validation done
Class Validation extends CI_Model{
function __construct(){
parent::__construct();
}
function useraccess($user,$pswd)
{
$query = $this->db->query("select * from user where username='$user' AND password='$pswd'");
foreach ($query->result_array() as $row)
{
if($row['username']==$user AND $row['password']==$pswd)
{
return true;
}
else
{
return false;
}
}
}
}
here is a view
login page
create 2 text box and 1 submit button and declare form action as
localhost/index.php/login/success
for logut
localhost/index.php/login/logout

Code Igniter - Unable to load requested file after redirect

I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result

codeigniter: call Index Method from another method

I am new in CodeIgniter. I am getting a problem. My procedure seems logical but it would not work!
I have a controller: User
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index($slug = FALSE) {
$data['title'] = "User List";
if($slug === FALSE) {
$data['user'] = $this->user_model->get_user();
} else {
$data['user'] = $this->user_model->get_user($slug);
}
if (empty($data['user'])){
show_404();
}
$this->load->library('table');
$this->load->view('user_view', $data);
}
public function authen() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('phone', 'phone', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('login_form');
} else {
if($this->user_model->do_login()===TRUE) {
$this->index();
}
$this->authen();
}
}
}
The "authen" method is not working properly with its last conditions. Page does not redirect to controller.
Can anybody help?
Thanks
You can use: redirect('user') it will redirect u to the controller user class
Instead of
$this->index();
try this
header('Location:http://myipaddress/mysite/user');
You are try to load the login page in case there is any authentication error.
For for this you will need to bring user to the login page again.
use redirect('user/login'); and store the error message in session flash.
or display validation errors..
you cannot call controller from a controller. as $this->index();

Resources