Codeigniter重定向()问题与$ _session

时间:2017-02-09 10:47:31

标签: php codeigniter

我在post_ads()方法中生成随机数,然后将其存储在$_SESSION['temp_id']中,只需使用post_as_offline()简单调用redirect('ads/post_as_offline', 'refresh' )方法,$_SESSION['temp_id']更改为替换为新的随机数。为什么重定向后,rand()调用并重新生成并存储新值?

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    class ads extends CI_Controller {
    private $Data;
    public function post_ads()
    {
     $this->common_data();
     $rand = rand(); 
     $_SESSION['temp_id'] = $rand ; // example $_SESSION['temp_id'] = 1000 ; 
     if(iSset($_POST['add']))
        {
        // some code  
         redirect('ads/post_as_offline' , 'refresh');
        }
       else
       {
       $this->load->view('add_ads_step2' , $this->Data);
       }

    }
    public function post_as_offline()
    {   
       $this->common_data();
       // will be $_SESSION['temp_id'] = 52635 ; rand() regenerat value after redirect 
       $this->load->view('post_as_offline_step',$this->Data);
    }

    }

2 个答案:

答案 0 :(得分:1)

可能是您的会话在发布后更改。试试这个:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    class ads extends CI_Controller {
    private $Data;
    public function post_ads()
    {
     $this->common_data();
        if(iSset($_POST['add']))
        {
        // some code  
         redirect('ads/post_as_offline' , 'refresh');
        }
       else
       {
        $rand = rand(); 
        $_SESSION['temp_id'] = $rand ; // example $_SESSION['temp_id'] = 1000 ; 

        $this->load->view('add_ads_step2' , $this->Data);
       }

    }
    public function post_as_offline()
    {   
       $this->common_data();
       // will be $_SESSION['temp_id'] = 52635 ; rand() regenerat value after redirect 
       $this->load->view('post_as_offline_step',$this->Data);
    }

    }

答案 1 :(得分:0)

当我在重定向之前和之后随机检查相同。你可以通过进入url会话值来轻松检查。我在我的测试页面上检查了你的代码只是删除了常见的数据方法。请检查您在发布的问题中是否遗漏了某些内容。 示例: -

private $Data;
public function post_ads()
    {
        $rand = rand();
        $_SESSION['temp_id'] = $rand ; // example $_SESSION['temp_id'] = 1000 ;
        if(isset($_SESSION))
        {
            redirect('test/post_as_offline?code='.$_SESSION['temp_id'] , 'refresh');
        }
        else
        {
            $this->load->view('add_ads_step2' , $this->Data);
        }

    }
    public function post_as_offline()
    {
        print_r($_SESSION);die;
        $this->load->view('post_as_offline_step',$this->Data);
    }