codeigniter登录重定向太多到家庭控制器

时间:2017-08-07 03:52:21

标签: php codeigniter

我的登录和视图有一个问题,我希望一旦用户登录并退出会话然后将我重定向到我家,但是用户将网址滑到根项目或登录网址然后它应该保持home。,我的代码看起来像这样

MY_Controller

**records_path_queue = tf.train.string_input_producer(records_path, seed=123,num_epochs=num_epochs,name="string_input_producer")**
reader = tf.TFRecordReader()
_, serialized_example = reader.read(records_path_queue,name="serialized_example")
......
image = tf.cast(image, tf.float32) * (1.0 / 255) - 0.5
label = tf.cast(features["label"], tf.int32)
**images, labels = tf.train.shuffle_batch([image, label], batch_size=batch_size, name="shuffle_bath",capacity=200 + 2 * batch_size, allow_smaller_final_batch=True,min_after_dequeue=100)**
......
init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
    while not coord.should_stop():
        .......
except tf.errors.OutOfRangeError as e:
    print("Finished training for %d epochs %d steps" % (Flags.num_epochs, global_step))
finally:
   coord.request_stop()
coord.join(threads=threads)
sess.close()

登录

protected function isLogged(){
        if (!$this->session->userdata('log'))
            redirect('login');
    }

家庭控制器

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

class Login extends MY_Controller {
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $data['module'] = 'Login';
        $this->load->view('header',$data);
        $this->load->view('login');
    }

    public function getAccess(){
        $username = $this->input->post('username', TRUE);
        $password = $this->input->post('password', TRUE);
        $result = $this->user->login($username,$password);
        if (!empty($this->input->post('username')) && !empty($this->input->post('password'))) {
            if (!$result) {
                $this->json(array('error' => 'invalid username or password'));
            }else{
                $data_session = array(
                    'id' => $result['id'],
                    'first_name' => $result['first_name'],
                    'last_name' => $result['last_name'],
                    'type' => $result['profile_id'],
                    'logged_in' => TRUE 
                );
                $this->session->set_userdata('log',$data_session);
            }
        } else {
            $this->json(array('empty' => 'You did not fill out the required fields.'));
        }
    } 

    public function logout(){
        $this->session->sess_destroy();
        redirect('login','refresh');
    }
}

2 个答案:

答案 0 :(得分:1)

你可以尝试

$this->login();

redirect(base_url());

上次我通过在帮助文件中创建islogged()函数解决了这个问题。

答案 1 :(得分:1)

您可以查看此代码可能有用

这是我的登录控制器

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

class Login extends CI_Controller {
function __construct()
{
    parent::__construct();
    $this->load->helper(array('url','form','file'));
    $this->load->model(array('admin_model'));

    //print_r($_SESSION);
    // print_r($data['all_cats']);die();

}
public function index()
{
    if($this->input->post('submit')!='')
    {
        $email = $this->input->post('email');
        $pwd = $this->input->post('password');
        $login  = $this->admin_model->login($email,$pwd);
        if(!empty($login))
        {
            redirect('Welcome/dashboard');
        }
        else
        {
            $this->session->set_flashdata('error', 'Incorrect Login Details');
            $this->load->view('login');
        }
    }
    else
    {
        $auth = checkAdminLogin();
        if( $auth == 1) 
        {
            redirect('Welcome/dashboard');
        }
        else
        {
            $this->load->view('login');
        }
    }
}
}

那是我的欢迎控制器

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

class Welcome extends CI_Controller {
function __construct()
{
    parent::__construct();
    $this->load->helper(array('url','form','file'));
    $this->load->model(array('admin_model'));
    $data['dbname']=$this->admin_model->get_db_name();
    $data['all_cats']=$this->admin_model->get_categories();
    $data['websites']=$this->admin_model->website_type();
    $data['sub_cats']=$this->admin_model->get_sub_cats();
    $admin_id=$this->session->userdata('adminId');
    if($admin_id == '')
    {
        redirect(base_url());
    }
    $this->load->vars($data);
    //print_r($_SESSION);
    // print_r($data['all_cats']);die();

}

// Admin Login Page 

 // End Function

// Redirect to Admin DashBoard
public function dashboard()
{
    $auth = checkAdminLogin();
    if( $auth == 1) 
    {
        $data = array();
        $this->load->view('index',$data);
    }
    else 
    {
        redirect('Welcome');
    }
}
// End Function

 // Logout Function 
public function logout()
{
    $this->session->unset_userdata('adminId');
    $this->session->sess_destroy();
    redirect('Login');
}
// End Function

// Manage Blog Categories
public function blog_categories($reload)
{

    $data['list']=$this->admin_model->blog_categories();
    if(isset($reload) && $reload !='')
    {
      $this->load->view('datatable',$data);
    }
    else{
    //print_r($data['list']);die();
    $this->load->view('users-list',$data);
}
}