Welcome Guest, Not a member yet? Register   Sign In
ci session problem
#1

[eluser]kosminbonea[/eluser]
Hi, i've been using CI for several applications, and everything is working just great, no problems whatsoever. Until now.
I have a controller for a product, with several methods to display product description, requirements, price quote and an application tour. Now, customers can view everything without being logged in, but when they want to place an order, they need to authenticate.
In order to keep them on the same page, I store the current page in the session userdata, so after they go through the login process (validation, verifying the account, etc.), I get the currentPage from session and redirect there, in order for them to end up in the same place.
I need to do this because they can get to the authentication page from a number of locations, they might enter wrong data, already have an order form filled, etc.
Problem is, in my tour method, the current page doesn't get set in the session, even if it does the exact same thing as in all the other methods. This is what the controller looks like:

Code:
<?php
require_once('constants.php');
class Product extends Controller
{
    function Product
    {
        parent::Controller();
        $this->load->model('Order_model');
        $this->load->model('Client_model');
        $this->load->model('Operation_model');
    }
    
    function index()
    {
        redirect('product/description', 'location');
    }

    function description()
    {
        $this->session->set_userdata('currentPage', 'product/description');
        $data['meta_title'] = 'Product :: Description';
        $this->load->view('description', $data);
    }
    
    function testimonials()
    {
        $this->session->set_userdata('currentPage', 'products/testimonials');
        $data['meta_title'] = 'Product :: Testimonials';
        $this->load->view('testimonials', $data);
    }
    
    function tour($pageNumber) // gets the page number from the third segment
    {
        if($pageNumber)
        {
            echo $pageNumber; //this was for testing - gets it right
            $pg = 'product/tour/'.$pageNumber; // also right
            $this->session->set_userdata('currentPage', $pg);
            echo $this->session->userdata('currentPage'); // also right
            $data['meta_title'] = 'Product :: Tour - page '.$pageNumber; // obviously, the title displays correctly
            $this->load->view('tourpage'.$pageNumber, $data); // it even loads the correct view
        }
        else
        {
            redirect('product/tour/1', 'location');
        }
    }
}
?>

No syntax errors, I triple checked, a colleague double checked, and I checked again with the editor. No errors or warnings reported, and yet when I check the cookie with the firefox web developer toolbar, the currentPage is the one set by the last method visited EXCEPT the tour.
After logging in, or out, that's where the customer ends up, on the last page they visited before or after the tour.
I'm using CI 1.5.4, not using database sessions, the cookie is well under 4KB, there is data stored correctly after currentPage, everything else works fine, except the set_userdata() in the tour method. This is the first time this happens, and I've used ci session extensively. I've flushed cache, cookies, temporary files, everything I could think of, to no avail.
I suppose I could use PHP sessions or rewrite some code and be done with it, but I'm really annoyed by this, especially since I can't for the life of me tell what's wrong.
Any ideas? Any help much appreciated.
#2

[eluser]xwero[/eluser]
I think it's safer to work with the segment method of the uri class to get a segment. To do this you have to change the tour method to
Code:
function tour()
{
   if(!$this->uri->segment(3))
   {
       redirect('product/tour/1', 'location');
   }
   else
   {
      $pageNumber = $this->uri->segment(3); //this was for testing - gets it right
            $pg = 'product/tour/'.$pageNumber; // also right
            $this->session->set_userdata('currentPage', $pg);
            echo $this->session->userdata('currentPage'); // also right
            $data['meta_title'] = 'Product :: Tour - page '.$pageNumber; // obviously, the title displays correctly
            $this->load->view('tourpage'.$pageNumber, $data); // it even loads the correct view
   }
}
#3

[eluser]kosminbonea[/eluser]
Thanks for the quick reply, but that didn't work either.
I just noticed this:
Code:
$this->session->set_userdata('currentPage', 'string'.$variable); // doesn't work
$this->session->set_userdata('currentPage', $variable); // doesn't work
$this->session->set_userdata('currentPage', 'string'); // works
This is really weird.
#4

[eluser]xwero[/eluser]
That is weird. Have you tried it with an array?
Code:
$data = array('currentPage'=> 'string'.$variable);
$this->session->set_userdata($data);
#5

[eluser]kosminbonea[/eluser]
I tried with the array just before trying with string. But now, what do you know, it works normally, that is
Code:
set_userdata('currentPage', $pg)
.
Must have been some glitch on my host server. I don't know what else could it be, since every other method in the controller worked as expected. I don't use routes or anything else I can think of that could interfere. I'm curious if it will happen again (hope not).
Thanks a lot for your time.




Theme © iAndrew 2016 - Forum software by © MyBB