Welcome Guest, Not a member yet? Register   Sign In
$_POST data not passing through controllers
#1

[eluser]mattpointblank[/eluser]
Hi everyone.

I'm writing a function to allow user interaction with my site. The idea is that if someone tries to take an 'action' (eg post a comment, rate a photo, etc), if they're not logged in, I redirect them to a register/login page. Once they've logged in, I send them back to the page they were posting a comment on, with the comment they typed already in the textarea.

The comment form points to this function (in my 'article' controller)

Code:
function addComment()
    {    
        $this->load->helper('url');
        if(!$this->session->userdata('logged_in')) {
            $newdata = array(
                'comment' => $this->input->post('comment')
            );
            $this->session->set_userdata($newdata);
            redirect('/user/authenticate');
        } else {
            // TODO: add their comment to database here
        }
    }

This then redirects them to the /user/authenticate function, which logs them in. The login function then sends them back to the original page. The comment, however, doesn't get stored. From what I can gather, CodeIgniter destroys the $_POST array after changing controllers? I set the global XSS filter to FALSE but still nothing. Can anyone help? When I tried replacing the $_POST value with a 'hello world' string, it worked fine.
#2

[eluser]Maglok[/eluser]
Flashdata form sessions can be used, see the user_guide.
#3

[eluser]mattpointblank[/eluser]
This doesn't seem to work - I tried changing the function above to set some flashdata containing the $_POST value, then assigned this flashdata value to a userdata value in the /user/authenticate function (which works). But once my /user/authenticate/ function redirects to /user/login/ it loses the comment data again. Again, if I just use a hardcoded string as the 'comment' value, it works fine.
#4

[eluser]mattpointblank[/eluser]
Hmm, experimenting a little here. My flow is like this:

Post comment at /article/123 -> redirect to /user/authenticate -> form posts to /user/login -> redirected back to /article/123

I just tried skipping the second step, so the flow goes to

Post comment at /article/123 -> redirect to /user/login

On /user/login I echoed out the comment userdata - it doesn't print using the first flow, but it does with the second. What could be wiping it in /user/authenticate/? All that page does is load some views with the forms.
#5

[eluser]bretticus[/eluser]
post is one transaction or one trip to the server. You cannot possibly end up with the post data several trips later because http is a stateless protocol. To get around this "stateless-ness" we use cookies. When you have a cookie that can reference data stored on the server somewhere, that is called a session. You are right to use sessions to store the comment before redirecting to the login page. I'm not exactly sure why you don't have access to "comment" from...

Code:
$this->session->set_userdata($newdata);

...later when the user has authenticated. Perhaps you can show that code?

flash_data is useful because the data is cleaned up after the next transaction/trip. If you want to force it to persist until ylou can use it, you must use...

Code:
$this->session->keep_flashdata('comment');

...in the intermediary steps.
#6

[eluser]Zack Kitzmiller[/eluser]
I always have issues getting flashdata to work. I'm sure I'm just doing it wrong, but I have better luck with the [uglier]:
Code:
echo $this->session->set_userdata($array);
$this->session->unset_userdata($array);
#7

[eluser]mattpointblank[/eluser]
Fixed it: Firebug (Firefox addon) was breaking it. I've had issues with it recently before, where it messes with POST/GET data. At least it wasn't CI's fault...




Theme © iAndrew 2016 - Forum software by © MyBB