Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$_POST and $this->input->post() are empty #242

Closed
inigosola opened this issue Aug 21, 2011 · 34 comments
Closed

$_POST and $this->input->post() are empty #242

inigosola opened this issue Aug 21, 2011 · 34 comments

Comments

@inigosola
Copy link

When I send data in some forms $_POST is empty in the controller.
The method is POST and de location is right. In the controller I can't get the post values. $this->input->post() is empty as well.
I tried several versions of apache (1.3... and 2.2...).
I tried several versions of php (5.2... and 5.3...).
I tried with the lastest version of codeigniter.
I tried with Mac OS X SnowLeopard and Mac OS X Lion.
I tried with Gentoo (lastest version).
I tried with Windows XP.
I tried with MAMP and WAMP.
The error is the same.

@inigosola
Copy link
Author

I have used TAMPER DATA firefox plugin and the data is sent correctly. In controller there aren't data.

@bertiful
Copy link
Contributor

Not sure I completely understand the problem but when you say 'I send data in some forms' - I take it you're speaking of the HTML forms you use to submit a post request?

Have you tried adding $this->output->enable_profiler(); to your controller to see what is passed (if anything) to $_POST? Also, what's the 'error' you get?

@inigosola
Copy link
Author

Yes, HTML forms.
Yes, I tried adding $this->output->enable_profile(); and there aren't post data.

@jamierumbelow
Copy link
Contributor

Try adding this line to the top of your index.php file and seeing what the output is:

die(var_dump($_POST));

If you still don't get any POST data, the problem is with your form or your server, not CodeIgniter.

@bertiful
Copy link
Contributor

Agreed with jamierunbelow - I was thinking the same initially. Please paste the code for your form.

@inigosola
Copy link
Author

@jamierumbelow, this is the output:

array(4) { ["username"]=> string(11) "asd@asd.asd" ["password"]=> string(3) "asd" ["submit"]=> string(5) "Login" ["action"]=> string(16) "backoffice.login" }

@inigosola
Copy link
Author

@chrisberthe, this is de php code:

<?php echo form_open('backoffice/login','id="loginForm"'); ?> 
    <fieldset>
        <?php if( $this->session->flashdata('error_login') ) { ?><div class="error" id="error"><?php echo $this->session->flashdata('error_login'); ?></div><?php } ?>
        <legend><b><?php echo $this->lang->line('backoffice_school_login'); ?></b></legend>
        <p><?php echo $this->lang->line('backoffice_school_login_credentials'); ?></p>
        <input class="txt required email" type="text" name="username" value="<?php echo $this->lang->line('backoffice_your_email'); ?>" onfocus="if($(this).val() == '<?php echo $this->lang->line('backoffice_your_email'); ?>') $(this).attr({value:''});" onblur="if($(this).val() == '') $(this).attr({value:'<?php echo $this->lang->line('backoffice_your_email'); ?>'});"/><br/>
        <input class="txt required" type="password" name="password" value="<?php echo $this->lang->line('students_password'); ?>" onfocus="if($(this).val() == '<?php echo $this->lang->line('students_password'); ?>') $(this).attr({value:''});" onblur="if($(this).val() == '') $(this).attr({value:'<?php echo $this->lang->line('students_password'); ?>'});"/><br/>
        <div style="float: left;"><a href="<?php echo site_url('backoffice/reset_password'); ?>"><?php echo $this->lang->line('backoffice_forgot_password'); ?></a></div>
        <div style="float: right;"><?php echo form_submit('submit','Login'); ?></div>
        <br clear="all"/>
    </fieldset>
    <input type="hidden" name="action" value="backoffice.login"/>
<?php echo form_close(); ?>

And this is the generaed html code:

<form action="http://localhost/index.php?/backoffice/login" id="loginForm" method="post" accept-charset="utf-8"> 
    <fieldset>
                    <legend><b>Backoffice Login</b></legend>
        <p>Please enter your login credentials below:</p>
        <input class="txt required email" type="text" name="username" value="Enter your e-mail address" onfocus="if($(this).val() == 'Enter your e-mail address') $(this).attr({value:''});" onblur="if($(this).val() == '') $(this).attr({value:'Enter your e-mail address'});"><br>
        <input class="txt required" type="password" name="password" value="Password" onfocus="if($(this).val() == 'Password') $(this).attr({value:''});" onblur="if($(this).val() == '') $(this).attr({value:'Password'});"><br>
        <div style="float: left;"><a href="http://localhost/index.php?/backoffice/reset_password">I forgot my password</a></div>
        <div style="float: right;"><input type="submit" name="submit" value="Login"></div>
        <br clear="all">
    </fieldset>
    <input type="hidden" name="action" value="backoffice.login">
</form>

@bertiful
Copy link
Contributor

Judging from what the $_POST returned, the array contains appropriate values and isn't empty.

So the issue is something related to your application (my guess it's something in your backoffice controller and how you're manipulating that $_POST data), not CodeIgniter itself.

If anything, you can create your own repository, push your current project and we'll have a look from there.

@inigosola
Copy link
Author

The project is not mine and I can't upload to a public repository. I'm sorry.
But I can put the snippets of code that you wish.

This is the login method from backoffice controller:

function login() {
            // here $_POST and $this->input->post() are empty
    if($this -> input -> post('action') == "backoffice.login") {
        $username = $this -> input -> post('username');
        $password = $this -> input -> post('password');

        if($user_id = $this -> user_model -> authenticate($username, $password)) {
            $this -> session -> set_userdata('logged_in', true);
            $this -> session -> set_userdata('user_id', $user_id);

            redirect('backoffice');
        } else {
            $this -> session -> set_flashdata('error_login', '<p>User and password doesnt match.</p>');
            redirect('backoffice/login');
        }
    }

    $data['page'] = "login";

    $this -> load -> view('header', $data);
    $this -> load -> view('backoffice/login', $data);
    $this -> load -> view('footer', $data);
}

@bertiful
Copy link
Contributor

The problem lies with your controller. Make sure you run a check on the form submission to see whether or not the form passed validation using:

$this->form_validation->run() == FALSE

You should also set some rules for each of your fields. Everything you need for form validation can be found in the CodeIgniter documentation located here: http://codeigniter.com/user_guide/libraries/form_validation.html.

@gaker
Copy link
Contributor

gaker commented Aug 21, 2011

as chrisberthe said, this looks to be a controller issue, not a CI issue. So closing this one out.

@gaker gaker closed this as completed Aug 21, 2011
@inigosola
Copy link
Author

I don't think so. The project works in a server but not in my laptop.

@inigosola
Copy link
Author

@chrisberthe, I tried $this->form_validation>run() == FALSE. The problem is the same. The validations rules works when I don't fill the fields, but when I fill the fields and I try getting de value from $this->input->post('username'), I don't get the value.

@inigosola
Copy link
Author

$this->form_validation->run() ALWAYS returns bool(false).

@bertiful
Copy link
Contributor

I tested the code both locally and remotely - works fine for me. Try this for both your controller and view, respectively:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backoffice extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        $this->output->enable_profiler(TRUE);

        $this->load->helper('url_helper');
        $this->load->library('form_validation');
        $this->load->library('session');
    }

    public function index()
    {
        redirect('backoffice/login', 'refresh');
    }

    public function login()
    {
        if ($this->input->post('action') == 'backoffice.login')
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $data['username'] = $username;
            $data['password'] = $password;
        }

        $data['title'] = 'Wtf Title';

        $this->load->view('backoffice/login', $data);
    }
}
<!DOCTYPE html>
<head><title><?php echo $title; ?></title></head>

<body>
<?php echo form_open('backoffice/login','id="loginForm"'); ?> 
    <fieldset>
        <legend><b>Login Form</b></legend><br>
        <span style="font-size: 1.5em"><?php echo (isset($username) AND isset($password)) ? $username .' / '. $password : ''; ?></span>
        <p>Please enter your credentials</p>
        <input class="txt required email" type="text" name="username" value="" onfocus="if($(this).val() == 'Email') $(this).attr({value:''});" onblur="if($(this).val() == '') $(this).attr({value:'Email'});"/><br/>
        <input class="txt required" type="password" name="password" value="" onfocus="if($(this).val() == 'Password') $(this).attr({value:''});" onblur="if($(this).val() == '') $(this).attr({value:'Password'});"/><br/>
        <div style="float: left;"><?php echo anchor('backoffice/reset_password', 'Reset Password'); ?></div>
        <div style="float: right;"><?php echo form_submit('submit','Login'); ?></div>
        <br clear="all"/>
    </fieldset>
    <input type="hidden" name="action" value="backoffice.login"/>
<?php echo form_close(); ?>
</body>
</html>

If there's still a problem, look into your localhost configuration. Sounds like it's the server if it works remotely for you. Also, since this issue is now closed, you're best bet is to move this to the CodeIgniter help forums - you'll have more luck there.

@inigosola
Copy link
Author

ok, your code works in my laptop. The problem is the $this->session. I can't get the flash value. The data session is lost but it is stored in the database. Why?

@bertiful
Copy link
Contributor

I'm not too sure - I'd have to see the rest of the code as it doesn't make sense to me right now.

Like I said, you should post your enquiry to the CodeIgniter help forums. You'll have a better chance of finding a solution to your problem as this thread is no longer open here.

@wallern
Copy link

wallern commented Feb 15, 2012

Hi, same issue here. The code worked fine in windows, when I change the environment to Ubuntu, I couldn't get any value passed from my view form. When I use the above suggested methods: die(var_dump($_POST)); and $this->output->enable_profiler(); I get:
array(0) {}

POST DATA
NO POST DATA EXISTS

I'm pretty sure my form is ok as I tested it in windows, my question is if this is apache issue, how could I resolve it?
Thanks.

@unusorin
Copy link

unusorin commented Jul 4, 2012

same problem here

@imtiazwazir
Copy link

same problem here too

@jaydlawrence
Copy link

Have the same problem.
Project worked on 3 different dev machines yet when set up on the server the post data is not accessible.
I also tried the die(var_dump($_POST)); in the index file and it showed that the post data is reaching codeigniter.

Turned out to be our authenticator failing in such a way that it would just redirect to the login page again and at that point the data was no longer present.

@pandeyamit
Copy link

I have also the same problem, I followed the same above but still problem is not solved.

@pandeyamit
Copy link

I got the solution, that is not CI issue or not a controller issue, its a issue of .htaccess setting.

@TheDigitalOrchard
Copy link

For the benefit of others that may experience this, was it a redirect of the request in the .htaccess to a slightly-different URI?

For example, if the .htaccess is set up to always append a training slash /, then any POST requests that originally point to the URI without the training slash will be redirected and you'll lose the POST data.

Or was it something else?

@pandeyamit
Copy link

You are right, and if Apache server is used then mod rewrite should be
enabled, otherwise redirection will be proper but with empty $_POST.

On Fri, Jan 18, 2013 at 2:02 PM, The Digital Orchard <
notifications@github.com> wrote:

For the benefit of others that may experience this, was it a redirect of
the request in the .htaccess to a slightly-different URI?

For example, if the .htaccess is set up to always append a training slash
/, then any POST requests that originally point to the URI without the
training slash will be redirected and you'll lose the POST data.

Or was it something else?


Reply to this email directly or view it on GitHubhttps://github.com//issues/242#issuecomment-12411954.

Thanks.

Best Regards !!

Amit Pandey
Sr. Software Engineer.
Soarlogic Information Technology Pvt. Ltd.

Mobile :- 9012474075
Mail to:- amit.pandey@soarlogic.com

@ghost
Copy link

ghost commented Jan 25, 2013

Pandeyamit has it. After a fresh install of WAMP I experienced this problem. The key was enabling mod_rewrite again.

@nardev
Copy link

nardev commented Sep 20, 2013

I have tipical .htaccess for codeigniter, and one particular rule is amking me problems. If it is not commented web doesn't work (can't do any analytics through logs) and if it is comented, it doesn't pass any POST variables.

=++++++++++++++++++++++++++++++++++++++++++++++++++++++

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
THIS ONE IS MAKING PROBLEMS -> # RewriteRule ^(.)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(ACT=.
)$ [NC]
RewriteRule ^(.)$ index.php?%1 [L]
RewriteCond $1 !^(images|css|style.css|favicon.ico|robots.txt|index.php) [NC]
RewriteRule ^(.
) /index.php?/$1 [L]


@bayusuwarninghadi
Copy link

it maybe mod_rewrite was disabled, try type this on your console
#sudo a2enmod rewrite

@camilord
Copy link

hi.. i got same problem and solved it..

cd /etc/apache2/mods-enabled/

locate mod_rewrite

/usr/lib/apache2/modules/mod_rewrite.so

touch rewrite.load

nano rewrite.load

@file: rewrite.load

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

then save...

restart your apache...

hope it will work at your end.

My.Env: Kali Linux (Debian)

@vellaimary
Copy link

Hi @pandeyamit , You are right ... Its just a URL redirecting issue . I was almost hitting my head for this problem ... My form action url and the redirected url were deffered coz of htaccess . Thank You

@craigsailor
Copy link

Hi, hoping to save others some time with what I learned.
On an IIS server, if you don't have a trailing slash on your URL the server will send a 301 redirect which will interfere with POST data.
I banged my head against the wall for quite a while before stumbling across that tidbit of knowledge.

@ghost
Copy link

ghost commented Oct 15, 2015

Same here. Solved with "sudo a2enmod rewrite" :)

@NilsRenaud
Copy link

same problem... same solution :)

@rohanjha1992
Copy link

Use the following two command in your terminal

  1. sudo a2enmod rewrite
  2. service apache2 restart
    This is Rewriting issue but i am almost sure it will work for you. :)

@bcit-ci bcit-ci locked and limited conversation to collaborators Nov 2, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests