简体   繁体   中英

Redirect to Login View from Controller In Code-igniter Error occured

Hi I'm new to php and code igniter. what I tried is to get login info from view and validated user and need to send a message to user whether login details are incorrect.

Controller Code :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct() {
    parent::__construct();

    $this->load->model('login_model');
    $this->load->helper('form'); 
    $this->load->helper('url');

    }
    public function index()
    {
        //$this->load->helper('url');
        $this->load->view('login/login');
    }
    public function login_check()
    {
        //$this->load->view('hello');
        //echo "directed";
    $user_id = $this->input->post('usernm');
    $userPassword = $this->input->post('passwordd'); 
    //echo $user_id.' and'.$userPassword;
    $var = $this->login_model->check_login($user_id);
    $status = 0;
    if(empty($var))
    {
        echo "Invalid user";
        $status = 0;
    }
    else
    {
    //echo var_dump($var);
    $username = $var->username;
    //echo $username;
        $status = 1;

    }
  $this->load->helper('url');
  redirect('login/login');
  //$this->load_>view('login\login');// at this point it does not redirect to login page and instead of that displaying error 404.page not found.
//echo $status;

    }

}

Same url ,earlier loaded when it called from from index function but fi it is calling from login check()function does not directed to the view and displaying error 404 page not found.

Any assistance regarding this world be a great help. Thanks a Lot!

You need to write redirect('welcome'); instead of redirect('login/login'); . Because login/login is the view page and you are trying to redirect direct on view without using controller. So you have two option which I had written bellow.

  1. redirect('welcome');
  2. $this->load->view('login/login');

My suggestion is please choose 1st option because 2nd option already you have implemented in first one.

I hope this one will work on you.

    public function login_check()
    {
       $user_id = $this->input->post('usernm');
       $userPassword = $this->input->post('passwordd'); 
       $var = $this->login_model->check_login($user_id, $userPassword); //pass username and password to your model to authenticate if input exists.
       $data['error'] = '';
    if(!empty($var)) //check if var is not empty
    {
        $this->session->set_userdata($var); //set user_data to var
        redirect('Account/page');  //redirect it to your account or success page
    }else{
         $data['error'] = 'Invalid Username or Password.';
         }
        $this->load->view('login/login',$data); //pass the error notification to your page.
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM