CodeIgniter Tempdata – Tempdata In CodeIgniter With Example
In this post we will show you how to implement CodeIgniter Tempdata and Tempdata In CodeIgniter With Example, CodeIgniter likewise bolsters “tempdata”, or session information with a particular lapse time. After the esteem lapses, or the session terminates or is erased, the esteem is consequently evacuated.
Thus to flashdata, CodeIgniter Tempdata factors are consistent session vars that are set apart particularly under the '__ci_vars'
key (once more, don’t touch that one).
To stamp a current thing as “tempdata”, essentially pass its key and expiry time (in seconds!) to the mark_as_temp()
strategy:
// 'result' will be erased after 360 seconds $this->session->mark_as_temp('result', 360);
You can check various things as CodeIgniter Tempdata in two courses, contingent upon whether you need them all to have a similar expiry time or not:
// Both 'result' and 'result2' will expire after 332 seconds $this->session->mark_as_temp(array('result', 'result2'), 332); // 'result' will be erased after 360 seconds, while 'result2' // will do so after only 180 seconds $this->session->mark_as_temp(array( 'result' => 360, 'result2' => 180 ));
To include CodeIgniter Tempdata:
$_SESSION['result'] = 'value'; $this->session->mark_as_temp('result', 360); // Expire in 6 minutes
Or, then again on the other hand, utilizing the set_tempdata()
technique:
$this->session->set_tempdata('result', 'value', 360);
You can likewise pass an exhibit to set_tempdata()
:
$tempdata = array('newuser' => TRUE, "message" => 'A debt of gratitude is in order for joining!'); $this->session->set_tempdata($tempdata, NULL, $expire);
Note :: If the lapse is excluded or set to 0, the default time-to-live estimation of 360 seconds (or 6 minutes) will be utilized. To peruse a CodeIgniter Tempdata variable, again you can simply get to it through the $_SESSION
superglobal exhibit:
$_SESSION['result']
Critical :: The userdata()
technique won’t return CodeIgniter Tempdata things. Or, then again in the event that you need to make certain that you’re perusing “tempdata” (and no other kind), you can likewise utilize the tempdata()
strategy:
$this->session->tempdata('result');
What’s more, obviously, on the off chance that you need to recover all current tempdata:
$this->session->tempdata();
Note :: The tempdata()
strategy returns NULL if the outcome can’t be found. On the off chance that you have to expel a CodeIgniter Tempdata esteem before it terminates, you can specifically unset it from the $_SESSION
cluster:
unset($_SESSION['result']);
In any case, this won’t expel the marker that makes this particular outcome to be CodeIgniter Tempdata (it will be negated on the following HTTP ask for), so in the event that you mean to reuse that same key in a similar demand, you’d need to utilize unset_tempdata()
:
$this->session->unset_tempdata('result');
Remove CodeIgniter Tempdata
CodeIgniter Tempdata is evacuated naturally after its lapse time yet in the event that you need to expel CodeIgniter Tempdata before that, then you can do as appeared beneath utilizing the unset_tempdata()
work, which takes one contention of the outcome to be expelled.
$this->session->unset_tempdata('result');
Example for CodeIgniter Tempdata
Make a class called Tempdata_controller.php
and spare it in application/controller/Tempdata_controller.php
.
<?php class Tempdata_controller extends CI_Controller { public function index() { $this->load->library('session'); $this->load->view('tempdataview'); } public function add_tempdata() { // set session library $this->load->library('session'); // set url helper $this->load->helper('url'); // this tempdata "result" will be removed after 30 seconds $this->session->set_tempdata('result','result-value',30); redirect('tempdataexample'); } } ?>
Make a record called tempdataview.php
and spare it in application/sees/tempdataview.php
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter Tempdata Example - onlinecode</title> </head> <body> Temp Data Example <h2><?php echo $this->session->tempdata('result'); ?></h2> <a href = 'tempdataexample/add'>Click Here</a> to add temp data. </body> </html>
Roll out the improvements in the routes.php
record in application/config/routes.php
and include the accompanying line toward the finish of the document.
// set route $route['tempdataexample'] = "Tempdata_controller"; // set route add $route['tempdataexample/add'] = "Tempdata_controller/add_tempdata";
Execute the above case by going by the accompanying connection. Supplant the onlinecode
with the URL of your webpage.
Subsequent to going to the above URL, you will see a screen as demonstrated. we hope this post will help you for
CodeIgniter Tempdata.
You also like Remove index.php from url in codeigniter and CodeIgniter – Image and File Upload and and CodeIgniter UPDATE Query and Codeigniter Delete Query