Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Issues with Sessions
#1

[eluser]phused[/eluser]
I'm using sessions to store data that is being posted on a form, but when redirected to another page the information or data I have added using $this->session->userdata() is lost. I have a multi-step form/wizard that works this way:

1. Input Data (The form)
2. Preview Data (Preview page, if the user has to edit, they click back)
3. Confirmation (Just a success message page)

Once the user inputs their data on Step 1 and it passes validation this information they have inputed is stored using $this->session->userdata() and displayed on Step 2. Once on Step 2 if the information they have inputted is correct they can proceed to Step 3. However, once I get to step 3 I want to insert this data to SQL and I cannot pull out the information of the session/cookies.

I tried printing the values stored in the session on Step 3 and it returns blank. However, when I try to print CI's default ones which are session_id, ip_address and 2 others they seem to be there, it's just the custom ones that I have defined that are blank on Step 3.

Any idea why this is happening?
#2

[eluser]Thorpe Obazee[/eluser]
[quote author="Phused" date="1239858356"]I'm using sessions to store data that is being posted on a form, but when redirected to another page the information or data I have added using $this->session->userdata() is lost. I have a multi-step form/wizard that works this way:
*****
Any idea why this is happening?[/quote]
It's
Code:
$this->session->set_userdata('key', 'value');
not

Code:
$this->session->userdata()
#3

[eluser]phused[/eluser]
Sorry, I think I copied the wrong function, this is my code:

Code:
$session = array(
                        'title' => $this->input->post('title'),
                        'email' => $this->input->post('email'),
                        'company' => $this->input->post('company'),
                        'url' => $this->input->post('url'),
                        'cat' => $this->input->post('cat'),
                        'salary' => $this->input->post('salary'),
                        'desc' => $this->input->post('desc'),
                        'type' => $this->input->post('type'),
                        'location' => $this->input->post('location')
                        );
            
            $this->session->set_userdata($session);

(This happens at the end of Step 1)

I'm trying to retrieve the data and dump it to SQL this way:

Code:
$data = array(
                    'title' => $this->session->userdata('title'),
                    'email' => $this->session->userdata('email'),
                    'company' => $this->session->userdata('company'),
                    'url' => $this->session->userdata('url'),
                    'cat' => $this->session->userdata('cat'),
                    'salary' => $this->session->userdata('salary'),
                    'desc' => $this->session->userdata('desc'),
                    'type' => $this->session->userdata('type'),
                    'location' => $this->session->userdata('location'),
                    'ip' => $this->session->userdata('ip_address'),
                    'status' => '0',
                    );
        
        $this->db->insert('jobs', $data);

(This happens at the end of Step 3 and then the user is redirected to the homepage)
#4

[eluser]Thorpe Obazee[/eluser]
The weird thing is I just finished a multi-step form and I didn't get any problem with it.

Could you possibly be unsetting them?

Anyway,it's hard to say when we don't see more code.
#5

[eluser]phused[/eluser]
Here's some more of my code:

Code:
$this->form_validation->set_rules($validation_config);

        if ($this->form_validation->run() == FALSE)
        {
            // Return user back to form
            $this->load->view('header');
            $this->load->view('post');
            $this->load->view('sidebar');
            $this->load->view('footer');
        }
        else
        {    
            
            $session = array(
                        'title' => $this->input->post('title'),
                        'email' => $this->input->post('email'),
                        'company' => $this->input->post('company'),
                        'url' => $this->input->post('url'),
                        'cat' => $this->input->post('cat'),
                        'salary' => $this->input->post('salary'),
                        'desc' => $this->input->post('desc'),
                        'type' => $this->input->post('type'),
                        'location' => $this->input->post('location')
                        );
            
            $this->Preview($session);
        }
        
    }
    
    function Preview($session)
    {
        $this->session->set_userdata($session);
        
        $this->load->view('header');
        $this->load->view('preview');
        $this->load->view('sidebar');
        $this->load->view('footer');
    }
    
    function Confirm()
    {
        $data = array(
                    'title' => $this->session->userdata('title'),
                    'email' => $this->session->userdata('email'),
                    'company' => $this->session->userdata('company'),
                    'url' => $this->session->userdata('url'),
                    'cat' => $this->session->userdata('cat'),
                    'salary' => $this->session->userdata('salary'),
                    'desc' => $this->session->userdata('desc'),
                    'type' => $this->session->userdata('type'),
                    'location' => $this->session->userdata('location'),
                    'ip' => $this->session->userdata('ip_address'),
                    'status' => '0',
                    );
        
        $this->db->insert('jobs', $data);
        
        $this->load->view('header');
        $this->load->view('confirm');
        $this->load->view('sidebar');
        $this->load->view('footer');
    }
}

When I get to confirm, the data is lost.
#6

[eluser]Thorpe Obazee[/eluser]
From the looks of it, it should work.

However, I suggest you put the form data in a single session key so that you call it once when inserting to the database.

Can you show us how you 'preview' the session?
#7

[eluser]phused[/eluser]
Preview is just a view with the custom design of this website, I simply output the data they entered this way:

Code:
<?php echo $this->session->userdata('VALUE'); ?>

When I do this, the information I entered on the forms seems to display on the Preview page.

Also, what do you mean by putting the data in a single session key? I'm new to sessions so don't have much of an idea, if you can point me out to an article or something that would be great Smile

Thanks for the help so far!
#8

[eluser]Thorpe Obazee[/eluser]
Code:
$this->session->set_userdata('profile',  array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'address' => $this->input->post('address'),
                'address_2' => $this->input->post('address_2'),
                'city' => $this->input->post('city'),
                'email' => $this->input->post('email'),
                'country' => $this->input->post('country'),
                'home_phone' => $this->input->post('home_phone'),
                'cell_phone' => $this->input->post('cell_phone'),
                'postal_code' => $this->input->post('postal_code'),
                'age' => $this->input->post('age'),
                'creation' => date('Y-m-d')
            ));

Something like this.

then you save like this:
Code:
$this->db->insert('table', $this->session->userdata('profile'));
#9

[eluser]phused[/eluser]
I'm still having issues with this problem, what is happening now is it's setting all my custom defined values as 0.

Any idea why this is happening?
#10

[eluser]phused[/eluser]
I've also noticed that my session ID keeps changing when I move across different step on my form, I tried doing a search but only found some people saying the session class works and some others saying it doesn't. Here is what the table contents look like:

http://www.grabup.com/uploads/876e9cabbb...82a271.png

How can I solve the problem of keeping a single session_id generated by CI for longer?

Also, once a user get's to Confirm(), this is the data that is being dumped into SQL:

http://www.grabup.com/uploads/b6da7d40df...3b8cdd.png

As you can see, it keeps entering a 0 in all my fields.

Thanks for the help so far!




Theme © iAndrew 2016 - Forum software by © MyBB