0

ログインチェック機能を作成しています。しかし、2 つのフラッシュ データ メッセージが正しく設定されていません。

  1. ユーザーがログオンした後、セッションの有効期限が切れた場合、このフラッシュ データ メッセージを設定する必要があります。 セッション トークンの有効期限が切れました!
  2. また、ユーザーがログオンしておらず、ログオンせずにコントローラーにアクセスしようとすると、この flashdata メッセージを設定する必要があります。このサイトにアクセスするにはログインが必要です!

何らかの理由で、常に 2 番目の flashdata メッセージが表示されます。

質問:どうすれば 2 つの flashdata メッセージを適切に使用できますか?

コントローラー: Login.php 関数: check

public function check() {
    $uri_route = basename($this->router->directory) .'/'. $this->router->fetch_class();

    $route = isset($uri_route) ? $uri_route : '';

        $ignore = array(
            'common/login',
            'common/forgotten',
            'common/reset'
        );

        if (!in_array($route, $ignore)) {

            // $this->user->is_logged() returns the user id

            if ($this->user->is_logged()) {

                // $this->session->userdata('is_logged') returns true or false

                if (!$this->session->userdata('is_logged')) {

                    // Redirects if the user is logged on and session has expired!

                    $this->session->set_flashdata('warning', 'Your session token has expired!');

                    redirect('admin/common/login');
                }

            } else {

                $this->session->set_flashdata('warning', 'You need to login to access this site!');

                redirect('admin/common/login');

            }
    }

すべてのコントローラーに追加する必要がないように、 codeigniter フックを介して関数を実行します。

$hook['pre_controller'] = array(
        'class'    => 'Login',
        'function' => 'check',
        'filename' => 'Login.php',
        'filepath' => 'modules/admin/controllers/common'
);
4

1 に答える 1

1

uri() を介して何をチェックしようとしているのかは、実際には非常に悪いチェック方法です。また、単一ではなく構成関数としてログイン チェックを含める必要があります。関数は次のようになります。

    function __construct()
    {
        parent::__construct();
        if (!$this->session->userdata('logged_in')) {
        // Allow some methods?
            $allowed = array(
                'some_method_in_this_controller',
                'other_method_in_this_controller',
            );
            if (!in_array($this->router->fetch_method(), $allowed)
            {
            redirect('login');
           }
        }
    }
于 2015-11-21T05:15:55.647 に答える