I am in trouble using the CodeIgniter 2.2.6 session variables. I have a view, where some data is inputed by the user. Then, the user can press a submit button and this data goes to a web service, is processed and returned to the same view (using the controller and its model).
Bellow is the view code (v_index), where I save the data in the CodeIgniter session variables.
<?php
// loads the variables that will be used in print
$this->load->library('session');
$array_items = array(
'numberVehiclesUsed' => $numberVehiclesUsed,
'shortestRoute' => $shortestRoute,
'VRPSolution' => $VRPSolution,
);
$this->session->set_userdata($array_items);
?>
So, when I run the code bellow, I can see all the session data and everything is ok all the times. My application calls the web server, get the return and saves in the session variables everytime.
<?php
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
?>
So, I have another screen where I want to use the session data, called "Imprimir" (it means print). The controller code is bellow. The print_r is the for debuging proposes, offcourse.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_imprimir extends CI_Controller
{
function __construct()
{
parent::__construct();
//load the session library
$this->load->library('session');
}
public function index()
{
$dados['numberVehiclesUsed'] = $this->session->userdata('numberVehiclesUsed');
$dados['shortestRoute'] = $this->session->userdata('shortestRoute');
$dados['VRPSolution'] = $this->session->userdata('VRPSolution');
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
$this->load->view('v_imprimir', $dados);
}
}
?>
When I run the code for the first time, it saves the data in the session variables and I am able to load in the controller "Imprimir" as the code above. The print_r shows that all the data is there and ok.
The problem is when I run the first view (v_index) again with new data, the session variables are updated but, the controller "Imprimir" loads only the first (and old) data, and never updates it.
I have no idea what I aḿ doing wrong. The others Stackoverflow questions about problems with CodeIgniter session variables did not help me.
Any suggestion?
Related
My site uses multiple languages and my users can click on flags to set their desired language. When that flag is clicked, a Session should store that information and then i want my controller to redirect the user to another page. This i do with the following code:
<?php
public function setLangAction () {
$oLanguageCookie = new Container('language');
$oLanguageCookie->lang = $this->params ('langvar');
$this->redirect()->toRoute('loadpage', array('page' => 'home'));
}
?>
However, when i print_r($_SESSION) in the indexAction (the action where loadpage routes to), $_SESSION is empty.
Can somebody help me?
Depending where you param comes from you should execute
$this->params()->fromQuery('langvar');
$this->params()->fromPost('langvar');
unless it is a route parameter then you can use either:
$this->params()->fromRoute('langvar');
$this->params('langvar');
So I have this IF statement in my view. it works and all, but wondering if there's a better way to write this. Having both session calls seem unneccessary...
if($this->session->userdata('campaign_name')){
echo $this->session->userdata('campaign_name');
}
else {
echo 'this';
}
Note this function will be used inline on a text input. So I'm looking for as minimal code as possible.
Note that CI's Session class's userdata method will return false if no campaign_name exists. So assign a variable to the potentially undefined array key (campaign_name)
$campaign_name = $this->session->userdata('campaign_name');
if($campaign_name)
{
echo $campaign_name;
}
else
{
echo 'this';
}
OR
if($campaign_name = $this->session->userdata('campaign_name'))
{
echo $campaign_name;
}
Controller Method (/application/controllers/test_controller.php)
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Test_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function myFunction()
{
$data['campaign_name'] = $this->session->userdata('campaign_name');
$this->load->view('test_view',$data);
}
}
View (/application/view/test_view.php)
<html>
<head></head>
<body>
<input type="text" value="<?php echo $campaign_name; ?>">
</body>
</html>
In your controller, you can store the value of that session data into a variable and pass that along to the view. In the view, you can then have your if else statements that just look at the variable. I would advise against setting a variable in the view as RPM did. While it works, it breaks the separation of concerns you have going.
Also, look into using the alternative PHP syntax for views in CodeIgniter. It'll make your code neater and more maintainable.
Edit: I see now that RPM has updated his answer to set the variable in the controller. That's a good example to follow.
Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>
I have a codeigniter website that runs beautifully on localhost, but now that I have moved it to Host gator some page requests are taking around 30 seconds to serve up, if loaded at all! The weird thing is that is seemingly random, and while waiting for the page to load, if I simply re-click the link the page will load normally. I'm not sure if this is a programming problem in my controllers (code below) or just issues on host gators end. Please plesse please someone help me out here, as I am going insane.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller
{
function __construct()
{
parent::__construct();
if (!$this->session->userdata('language')) $this->session- >set_userdata('language', 'en');
}
function index ()
{
$tags['title'] = 'title tag';
$this->load->view($this->session->userdata('language').'/includes/view_header',$tags);
$this->load->view($this->session->userdata('language').'/company/view_company');
$this->load->view($this->session->userdata('language').'/includes/view_footer');
}
function warranty ()
{
$tags['title'] = 'title tag';
$this->load->view($this->session->userdata('language').'/includes/view_header',$tags);
$this->load->view($this->session->userdata('language').'/company/view_warranty');
$this->load->view($this->session->userdata('language').'/includes/view_footer');
}
}
I suggest you to test with codeigniter profiler, It will show all the processing time like sql execution etc...
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->output->enable_profiler(TRUE);
if (!$this->session->userdata('language')) $this->session- >set_userdata('language', 'en');
}
When enabled a report will be generated and inserted at the bottom of your pages.
To disable the profiler you will use:
$this->output->enable_profiler(FALSE);
For more details http://codeigniter.com/user_guide/libraries/output.html
Hope this will help you, let us know if anything there... Thanks!!
Do you have any Javascript or external file on your site? That might be the problem.
Try using Firebug/YSlow and you'll probably get if an external js/file (Facebook or Twitter js for example) is taking lot of time to load.
I have tried the following but it is giving me errors:
print_r($this->session->userdata());
How can I show all session data in CodeIgniter?
print_r($this->session->userdata);
or
print_r($this->session->all_userdata());
Update:
As of version 3.1.11 the above methods are deprecated. Use the below method to get all session data,
print_r($this->session->userdata());
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
Display yet formatting then you can view properly.
For print session data you do not need to use print_r() function every time .
If you use it then it will be non-readable format.Data will be looks very dirty.
But if you use my function all you have to do is to use p()-Funtion and pass data into it.
//create new file into application/cms_helper.php and load helper cms into //autoload or on controller
/*Copy Code for p function from here and paste into cms_helper.php in application/helpers folder */
//#parram $data-array,$d-if true then die by default it is false
//#author Your name
function p($data,$d = false){
echo "<pre>";
print_r($data);
echo "</pre>";
if($d == TRUE){
die();
}
}
Just remember to load cms_helper into your project or controller using $this->load->helper('cms'); use bellow code into your controller or model it will works just GREAT.
p($this->session->all_userdata()); // it will apply pre to your sesison data and other array as well
here is code:
<?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>'; ?>