簡體   English   中英

$ _session的Codeigniter redirect()問題

[英]Codeigniter redirect() issue with $_session

我生成隨機數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);
    }

    }

發布后可能會改變您的會話。 嘗試這個:

<?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);
    }

    }

正如我在重定向之前和之后對隨機檢查一樣,您可以通過進入會話值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);
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM