开发者

$this->input->post() always returning FALSE, due to an unwanted 301

开发者 https://www.devze.com 2023-03-06 05:22 出处:网络
I\'m working on a signup page in CodeIgniter. The problem is, my post data doesn\'t come through. The following code always produces \'fail\':

I'm working on a signup page in CodeIgniter. The problem is, my post data doesn't come through. The following code always produces 'fail':

class Signup extends CI_Controller {

    function index()
    {    
        if ($this->input->post())
        {
            echo 'success'; 
        }
        else
        {
            echo 'fail';

            echo form_open('signup');

            echo form_input('username', 'Username');
            echo form_input('email_address', 'Email address');
            echo form_submit('submit', 'Create Acccount');

            echo form_close();
        }
    }

}

What could be the problem?

To make matters even more interesting, on my localhost the form works just fine. It's when on the remote server when this fails.


Update 1: As requested, this is what the server outputs: (I obscured the url, I'm under non disclosure)

<form accept-charset="utf-8" method="post" action="http://www.url.com/signup">
<input type="text" value="Username" name="username">
<input type="text" value="Email address" name="email_address">
<input type="submit" value="Create Account"开发者_开发知识库 name="submit">
</form>

Update 2: I see another difference in behavior between the localhost and the remote server: When refreshing (cmd R or F5) the page after submitting the form, on the local host my browser asks me to send the form again. The same page on the remote server doesn't invoke that question from the browser so it looks like some redirecting or url problem is causing the problem?


Update 3: It appears that on the remote server, the user is being redirected by a 301. (moved permanently) I still have no idea where this redirect is coming from. The redirect effectively kills the post-data, so it explains why post() is returning false.

So, does anyone know why I'm being 301'd?


Update 4: I got redirected within CodeIgniter by setting my base_url as http://www.url.com in stead of http://url.com

After changing that, it solved the problem! :)


The problem could be caused by a 301 redirection (Location HTTP header directive) from the remote server. If you use Firefox, you could check for this with the plugin TamperData. Of course, you could also just see if http://www.url.com/signup gets redirected when requesting the page.

If the HTTP header Location is http://url.com/signup (without www), then the easiest solution is to use http://url.com/signup for the form action. Please note that you could also omit the server address if the form is on the same server. You could use /signup instead.


What version of CI are you using? CI Reactor made changes to $this->input->post()

Input Class methods post() and get() will now return a full array if the first argument is not provided.

I just tested a similiar condition; I'm using CI Reactor on localhost, and a pre-reactor version on my live, and got the same.

$_POST did contain data. Pre-reactor, $this->input->post doesn't return anything if there is no array passed:

$this->input->post() calls _fetch_from_array()

function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
{
    if ( ! isset($array[$index]))
    {
        return FALSE;
    }

However in reactor, this is done instead:

    if ($index === NULL AND ! empty($_POST))
    {
        $post = array();

        // Loop through the full _POST array and return it
        foreach (array_keys($_POST) as $key)
        {
            $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
        }
        return $post;
    }

So you get post values, even if you don't specify an index.


I'd start by print_r()'ing the $_POST/$_REQUEST superglobals to see if PHP is seeing anything. This should help narrow it to see if its a php or CI issue.


I had this problem its a weird fix i cant explain why but here is what worked. Don't use html to make the form use codeigniters form library. i tried it both ways and for some reason

$this->input->post('your_variable'); 

only worked when using the form class see codeigniter userguide

0

精彩评论

暂无评论...
验证码 换一张
取 消