douchun5969 2014-03-31 14:43
浏览 62
已采纳

Codeigniter - 在每个控制器功能中设置/检查会话

i would need to use the domain and put it in a session, to retrieve some data out of the database. I can do this in my default controller, no problem with that. The problem is when people link directly to a specific controller like: www.mywebsite/mycontroller/myfunction.

When they enter the website via this link, i don't set the session from the domain and i'll get some errors.

So my question is: Does someone have a solution to check/set this session in every function of every domain?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dpd7195 2014-03-31 14:51
    关注

    You can use a hook : http://ellislab.com/codeigniter/user-guide/general/hooks.html

    Or you can code your small script into the construct of your controllers.

    function __construct()
    {
        parent::__construct();
        //Put your code here, you can also load your models and stuff
        //$this->load->model("user_model","user");
        //Code code code
    }
    

    And here is an example of a hook for i18n (language module) -- Create a file under applications/hooks/my_hook.php

    function setUserLang()
    {
    //Getting the language of the user
    //If nothing was found, stick with English
    $ci =& get_instance();
    if(!$ci->session->userdata('lang')){
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        switch ($lang){
            case "fr":
                $ci->session->set_userdata('lang','french');
                break;
    
            case "en":
                $ci->session->set_userdata('lang','english');
                break;
    
            default:
                $ci->session->set_userdata('lang','english');
                break;
        }
    }
    }
    

    And go to applications/config/hooks.php and add something like :

    $hook['post_controller_constructor'][] = array(
                'class'     => '',
                'function'  => 'setUserLang',
                'filename'  => 'my_hook.php',
                'filepath'  => 'hooks'
                );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器