dongyao5186 2016-11-10 19:30
浏览 18
已采纳

如果会话存在,Codeigniter显示控制器

I'm new to codeigniter and learn it.

I got 2 controllers, 1st one called main and responds for user registration / login the second one is called todo and shows a todo list.

Now if I access localhost/list my website opens the page, where do I write the session logic to test if user is logged in?

My controller todo

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

class Lists extends CI_Controller {

public function index()
{
    $this->load->view('lists');}
}

How do I display it using the session:

if($this->session->userdata('is_logged_in') == 1)

Or do I have to put the session logic before each function?

  • 写回答

1条回答 默认 最新

  • dongpo1216 2016-11-10 19:36
    关注

    You put the session $this->session->userdata('is_logged_in') condition in __construct()

    Like this :

    <?php
    class Lists extends CI_Controller {
    
        public function __construct() {
            parent::__construct();
            if($this->session->userdata('is_logged_in') != 1) { 
                //redirect code here
            }
        }
    
        public function index() {
            $this->load->view('lists');}
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器