3

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! :)

18
  • What is the definition of $this->input->post() ? Commented May 15, 2011 at 10:53
  • @Chris Rasys - What do you mean? Commented May 15, 2011 at 10:54
  • This cannot be answered from the snippet you show. Install Xdebug and step through the request to find out where it's going wrong. On a sidenote: controllers should not echo. Commented May 15, 2011 at 10:56
  • 1
    what do you get if you try: $this->input->post("username")? Commented May 15, 2011 at 11:16
  • 2
    What happens if you do var_dump($_POST)? Anything there? Also, are you using a "preview" mode of any server control panel? Commented May 15, 2011 at 11:24

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

You found the problem! :D It gives me a '301 MOVED PERMANENTLY'. Very weird. Why is this happening? i have exactly the same code and install running on both the localhost and remote server.
@Kriem Without knowing the remote server configuration, it is hard to say, but my best guess is redirecting in httpd.conf or .htaccess files. It could be, for example, from http://www.url.com/signup to http://url.com/signup.
@Kriem You should have a look at the Location header. Compare it with the requested URI.
@Richard86 - the location response header says: http://url.com/signup just as requested.
@Kriem Are you positive that you aren't requesting http://www.url.com/signup instead of http://url.com/signup?
|
2

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.

5 Comments

I'm using Reactor 2.0.2. But to be completely honest, I'm not sure what you're trying to explain. :(
In pre-reactor CI; $this->input->post() returned false if nothing was passed to it; Reactor CI returns the whole $_POST array instead. Had you been using pre-CI on your live server, it may have explained the behavior you are experiencing (as it would always be false). But as you are using Reactor, I think the problem lies elsewhere. Does the output profiler shed any light? $this->output->enable_profiler(true);
This thing is a total nightmare. :S The profiler breaks everything. I see no output. Just a white page. I DO see another different behavior between the localhost and the remote server: When refreshing the page after submitting the form, on the local host my browser asks me to send the form data again. The same page on the remote server doesn't invoke that question from the browser. It looks like the from posting is destroyed?
Check your apache error log if you're getting a blank page then. At least for me, I can see $_POST data in the output profiling, even though it still fails
It appears to be a 301 problem.
0

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.

3 Comments

$_REQUEST is fine. $_POST is empty. (nothing there)
Doesn't CodeIgniter delete the content in $_POST?
@Jonas - I don't believe it does.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.