[英]Codeigniter - issue in setting flashdata
This is my code in a method: 这是我的方法代码:
public function index() {
$pathToFile = $this->session->flashdata( 'img_path' );
if ( $pathToFile ) {
$this->session->set_flashdata( 'file' , $pathToFile );
$this->session->set_flashdata( 'img_path' , $pathToFile );
$data[ 'pageTitle' ] = BLOGER_TITLE . ' دانلود تصاویر';
$data[ 'is_logedin' ] = $this->is_logedin();
$data[ 'banners' ] = $this->_getBanners( 'side' );
$data[ 'img_name' ] = basename( $pathToFile );
$this->load->view( 'download_image' , $data );
} else {
show_404();
}
}
Lets say user can't access this method unless having visited another method and setting the flashdata named img_path
there, and then redirect to this method. 可以说,除非访问了另一个方法并在那里设置了名为img_path
,然后重定向到该方法,否则用户无法访问此方法。 so far this method works perfect as expected, but if the user make a refresh on the page ,error 404 page should appear. 到目前为止,该方法可以按预期的那样完美地工作,但是如果用户刷新页面,则会出现错误404页面。 I want to keep the flashdata alive while user is refreshing current page so I should add a $this->session->keep_flashdata('img_path');
我想在用户刷新当前页面时保持flashdata保持活动状态,因此我应该添加$this->session->keep_flashdata('img_path');
in index method (or constructor, I tried both) to do so, or i should add another $this->session->set_flashdata('img_path',$pathToFile);
在索引方法(或构造函数,我都尝试过)中这样做,或者我应该添加另一个$this->session->set_flashdata('img_path',$pathToFile);
but both solution does't work for me. 但是这两种解决方案都不适合我。 Why? 为什么? for testing I put a var_dump($this->session->flashdata('img_path'));
为了测试,我放了一个var_dump($this->session->flashdata('img_path'));
in the end of method . 在方法的最后。 after redirect to this method I have the img_path but after refresh it doesn't the value so I tried setting the flashdata I in the method and I will have it and after refreshing the page it will set to null. 重定向到此方法后,我具有img_path,但刷新后没有该值,因此我尝试在该方法中设置flashdata,我将拥有它,刷新页面后,它将设置为null。 and also the other flashdata named file
never get the value assigned to it. 并且另一个名为file
flashdata也永远不会获得为其分配的值。 why is this happening? 为什么会这样呢?
my session config is as bellow and I loaded the session library in autoload.php
and it works fine in other controllers and pages. 我的会话配置如下所示,我将会话库加载到autoload.php
并且在其他控制器和页面中也可以正常工作。
$config['sess_expiration'] = 0;
$config['sess_save_path'] = APPPATH . 'cache/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
------------------------------------- -------------------------------------
UPDATE UPDATE
------------------------------------- -------------------------------------
A little simplicity in code: 代码简单一点:
public function test1() {
$this->session->set_flashdata( 'img_path',"abc" );
redirect('imgs/test2','refresh');
}
public function test2() {
$this->session->keep_flashdata( 'img_path' );
$pathToFile = $this->session->flashdata( 'img_path' );
if ( $pathToFile ) {
$this->session->set_flashdata( 'file' , $pathToFile );
$this->session->set_flashdata( 'img_path' , $pathToFile );
var_dump($pathToFile);
} else {
show_404();
}
}
When I go to localhost/imgs/test1
a it will set the flashdata and redirect me to localhost/imgs/test2
and the var_dump(...)
section will run and prints "abc" for me in details. 当我转到localhost/imgs/test1
,它将设置flashdata并将我重定向到localhost/imgs/test2
,并且var_dump(...)
部分将运行并为我详细打印“ abc”。 but when refreshing the page its show me 404 error page
because the flashdata is not exists anymore, even when I used keep_flashdata
. 但是刷新页面时会显示404 error page
因为即使我使用keep_flashdata
也不存在keep_flashdata
。
It should work with keep_flash data. 它应该与keep_flash数据一起使用。 Example: 例:
public function test() {
//From this request session will be set
$this->session->set_flashdata( 'img_path',"abc" );
redirect(base_url()."home/test");
}
public function index() {
$this->session->keep_flashdata('img_path');
$pathToFile = $this->session->flashdata( 'img_path' );
if ( $pathToFile ) {
$this->session->set_flashdata( 'file' , $pathToFile );
$this->session->set_flashdata( 'img_path' , $pathToFile );
$data[ 'pageTitle' ] = BLOGER_TITLE . ' دانلود تصاویر';
$data[ 'is_logedin' ] = $this->is_logedin();
$data[ 'banners' ] = $this->_getBanners( 'side' );
$data[ 'img_name' ] = basename( $pathToFile );
$this->load->view( 'download_image' , $data );
} else {
show_404();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.