Codeigniter:无法从一个控制器获取会话变量值

时间:2016-07-14 09:13:43

标签: php codeigniter session

我在autoload中包含了会话,并且它在所有其他地方工作。 我在检索会话变量时遇到问题,它返回null结果。在我设置会话的控制器中,它工作正常 这是我在其他情况下设置它的代码:

class Controller_catagory extends CI_Controller
{

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

        $this->load->model('generic_model');
        $this->load->model('backend/model_post');
        $this->load->model('backend/model_permissions');
    }

    public function index($param1='',$param2='') 
     {
        $id=$this->generic_model->getAllRecords('dramas',array(
            'drama_slug' => $param2 ),'drama_id','DESC');
        // print_r($id);
        if (!empty($id)) 
        {
            foreach ($id as $key) 
            {
                $id = $key['drama_id'];
            }
        }
        $data;

        if (!empty($param1) && empty($param2)) 
        {
            $data["page"]    = 'frontend/includes/view_alldramas';


            $id = $this->generic_model->getAllRecords('channel',array('channel_slug' => $param1 ),'channel_id','DESC');

            if (!empty($id)) 
            {
                foreach ($id as $key) 
                {
                    $id = $key['channel_id'];
                    $ch_slug= $data['ch_slug'] = $key['channel_slug'];
                    $this->session->set_userdata('ch_slug',$ch_slug);
                }
            }

            $this->session->set_userdata('channel_capture',$id);

            $data['dramas_pagination'] = $this->model_post->get_specific_channel_pagination(0,12,$id);
            $data["get_dramas"]=$this->model_post->get_all_dramas();

            $data['channels'] = $this->generic_model->getAllRecords('dramas', array('channel_fk' => $id ),'drama_id','DESC');
        }
        else
        {
            // The id is printing right result
            echo $id;

            // Here i'm setting session, if i retrieve here its working
            $this->session->set_userdata('drama_episode',$id);

            $data['episodes_pagination'] = $this->model_post->get_specific_post_pagination(0,12,$id);
            $data["get_episodes"]=$this->model_post->get_all_dramas();
            $data['dramas'] = $this->generic_model->getAllRecords('post',$arr = array(
            'dramas_fk' => $id ),'id','DESC');
            $data["page"]    = 'frontend/includes/view_allposts';

        }

        $data['title']   = 'GLOBAL VIDEOS';
        $data['heading'] = 'Dramas List';
        $data["top"]     = 'frontend/includes/top_home';


        $this->load->view('frontend/index',$data);
    }
  }

}

现在这是另一个类,我试图获取set session的值,但它没有检索数据,我得到空记录。

  

注意:我使用'channel_capture'做同样的事情,我成功获得了它的价值

class Home extends CI_Controller {

 public function __construct() 
{
    $data = array();
    parent::__construct();


    $this->load->model('backend/model_post');
    $this->load->model('generic_model');
}
public function ajax_posts()
{
    $start= $_GET['start'];

    // it gives empty result here don't know why
    $id = $this->session->userdata('drama_episode');

    //prints nothing
     echo "This key: ".$id;

    $post_pagination=$this->model_post->get_specific_post_pagination($start, 12, $id);
    var_dump($post_pagination);
    $str='';
    $base_url=base_url();

    if (empty($post_pagination))
    { 
        return false;
    }

    foreach($post_pagination as $post)
    {

        $str.=          '<div class="col-lg-3 col-sm-6 col-md-4 epi_height" >';
        $str.=              '<a href='.$base_url.$post['slug'].'>';
        $str.=              '<img class="img-responsive" src='.$base_url.$post['thumbnail'].' alt="recent dramas" />';
        $str.=              $post['title'];
        $str.=              '</a>';
        $str.=         '</div>';    

    }
        echo $str; 
   }
}

3 个答案:

答案 0 :(得分:1)

尝试在if之前设置会话,如:

$this->session->set_userdata('drama_episode',$id);

你的if if到这里然后检索它。

答案 1 :(得分:0)

从autoload.php自动加载会话库,如下所示:

$autoload['libraries'] = array('session');

您可以在任何地方使用会话变量!

或者您可能需要使用此处说明的闪光灯:

https://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata

答案 2 :(得分:0)

请执行以下问题排查: -

  1. 在index()函数内添加

    echo $ this-&gt; session-&gt; userdata(&#39; drama_episode&#39;);模具();

  2. 现在打电话给我相信网址/类别的控制器。

  3. 希望您能看到正确的会话值。
  4. 在ajax_posts()函数内打印整个会话

    的print_r($ _ SESSION);模具();

  5. 调用URL访问函数ajax_posts() 让我知道你得到的输出是什么?

  6. ==================================== 或者,在调用函数ajax_posts()之前检查是否有一些删除drama_episode的会话值的地方?