Welcome Guest, Not a member yet? Register   Sign In
$_POST and $this->input->post() are always empty on localhost site
#1

[eluser]ian[/eluser]
I've been working with CodeIgniter for about a month to maintain and update a pre-existing website. I had never noticed this problem until needing to update a login page and noticed that none of my forms that perform a POST(this also includes a contact page that sends an email) were working on my development site running on my Macbook. I can access variables in $_POST outside of the CI install, but when placing them inside of a CI controller, trying to access the variables either through $_POST or $this->input->post('varname') gives me 0 values. I suspect the culprit is either in my config file or my .htaccess. I am using mod_rewrite and have some rewrite conditions/rules, mostly just allowing me to remove index.php from my URL. I've checked my header activity using the Live HTTP Headers Firefox extension, but do not see any redirects (and I don't expect any) that might cause the POST data to be lost.

I'm accessing the site through a virtual host at http://url.dev that points to localhost.

My environment:
Mac OSX
Apache 2.2
PHP 5.3.4
CodeIgniter 1.7.2

This has been driving me absolutely crazy for a week, and I don't understand how something as simple as accessing POST data can be so hard to do and so hard to track down in CodeIgniter. Any thoughts for things I could try?
#2

[eluser]Atharva[/eluser]
Can you post your code
#3

[eluser]ian[/eluser]
My View:

Code:
<h2>&lt;?php echo lang('user_login_header') ?&gt;</h2>

&lt;?php echo form_open('users/login', array('id'=>'login')); ?&gt;
&lt;?php if (validation_errors()): ?&gt;
    <div class="error-box">&lt;?php echo validation_errors();?&gt;</div>
&lt;?php endif; ?&gt;
    
<ul class="reset">    

<li class="field">
    <label for="email">&lt;?php echo lang('user_email')?&gt;</label>
    &lt;input type="text" name="email" maxlength="120" value="&lt;?php echo $user_data-&gt;email; ?&gt;" />
</li>

<li class="field">
    <label for="password">&lt;?php echo lang('user_password')?&gt;</label>
    &lt;input type="password" name="password" maxlength="20" value="&lt;?php echo $user_data-&gt;password; ?&gt;" />
</li>

<li class="field checkboxes">
    &lt;?php echo form_checkbox('remember', '1', FALSE, 'class="checkbox"'); ?&gt; <label for="remember">&lt;?php echo lang('user_remember')?&gt;</label>
</li>
</ul>

<div class="buttons">
    &lt;input class="submit" type="submit" value="&lt;?php echo lang('user_login_btn') ?&gt;" name="btnLogin" /&gt;
</div>
    
&lt;?php echo form_close(); ?&gt;

<p>&lt;?php echo anchor('users/reset_pass', lang('user_reset_password_link'));?&gt; | &lt;?php echo anchor('register', lang('user_register_btn'));?&gt;</p>

My Controller Function:
Code:
public function login()
    {

        // Get the user data
        $user_data = (object) array(
            'email' => $this->input->post('email'),
            'password' => $this->input->post('password')
        );
        
        // Validation rules
        $this->validation_rules = array(
            array(
                'field' => 'email',
                'label' => lang('user_email_label'),
                'rules' => 'required|trim|callback__check_login'
            ),
            array(
                'field' => 'password',
                'label' => lang('user_password_label'),
                'rules' => 'required|min_length[6]|max_length[20]'
            ),
        );

        // Set the validation rules
        $this->form_validation->set_rules($this->validation_rules);
        
        // Set the redirect page as soon as they get to login
        if(!$this->session->userdata('redirect_to'))
        {
            $uri = parse_url($this->input->server('HTTP_REFERER'), PHP_URL_PATH);

            // If iwe aren't being redirected from the userl ogin page
            $root_uri = BASE_URI == '/' ? '' : BASE_URI;
            strpos($uri, '/users/login') !== FALSE || $this->session->set_userdata('redirect_to', str_replace($root_uri, '', $uri));
        }
        
        // If the validation worked, or the user is already logged in
        if ($this->form_validation->run() or $this->ion_auth->logged_in())
        {
            $redirect_to = $this->session->userdata('redirect_to')
                ? $this->session->userdata('redirect_to')
                : ''; // Home

            $this->session->unset_userdata('redirect_to');

            // Call post login hook
            $this->hooks->_call_hook('post_user_login');

            // Redirect the user
            redirect($redirect_to);
        }

        // Render the view
        $this->data->sBodyClass = 'one-col';
        $this->data->user_data =& $user_data;
        $this->template->build('login', $this->data);
    }

My .htaccess
Code:
php_value upload_max_filesize 32M
php_value post_max_size 32M
php_value max_execution_time 200
# Error Logging
php_flag display_errors on
php_value error_reporting 9

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine on

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    
    #RewriteBase /wherever/pyro/is

    # Keep people out of codeigniter directory and Git/Mercurial data
    RedirectMatch 403 ^/(application\/cache|codeigniter|\.git|\.hg).*$

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    <IfModule mod_php5.c>
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>

</IfModule>

I'll be happy to post anything from my config.php file, but I don't want to post the whole thing if not necessary.

I noticed I wasn't able to login to the page, so I initially thought it was a Session problem, but the first thing I tried was to print out the variables I was passing. Trying to print to the error log with
Code:
log_message('error', $this->input->post('email'));
gave me nothing.

When I put
Code:
var_dump($user_data);
after filling that array in the first few lines of the login() function, I get this printed out:
object(stdClass)#36 (2) { ["email"]=> bool(false) ["password"]=> bool(false) }
#4

[eluser]InsiteFX[/eluser]
Maybe if you move setting $user_data into the form_validation it just might work!

InsiteFX
#5

[eluser]ian[/eluser]
@InsiteFX

Can you explain why you think that might fix the problem? Before it can run the validation, it needs those 2 values in the $user_data array.

I should've mentioned that I don't think the logic of the code is the problem; it works on a remote production server, but isn't working for me on my local copy of the site. I also don't believe any of the validation code is in error. Validation is failing, but presumably because the POST value for email/password is empty/NULL (so, of course it's going to fail).
#6

[eluser]InsiteFX[/eluser]
Because you are setting them before they have been inputed and validated!

Until the form_validation has been run input values will be empty!

InsiteFX
#7

[eluser]osci[/eluser]
@ian Read this: form and $_POST problem on mac with MAMP. Maybe it helps you.

@InsiteFX Why wouldn't I have an input->post before validation? If I didn't have validation I wouldn't have post variables from my form?
#8

[eluser]InsiteFX[/eluser]
@osci, look at his code!
#9

[eluser]ian[/eluser]
@InsiteFX,

I'm still not sure how you recommend I change my code. Could you copy and paste the line(s) you think should change and show me where they should be?

@osci,

Thanks for the tip. It seems like those people were only having the problem when entering special characters. Unfortunately, that's not the case for me. I'm probably still going to downgrade my PHP to 5.2 to see if that helps and will report back with my findings.
#10

[eluser]osci[/eluser]
Quote:it works on a remote production server, but isn’t working for me on my local copy of the site.
He said that his code works in production environment so I didn't look at the code actually. And didn't read your post carefully, because I was thinking of mac incompatibilities. Soz about that.

@ian you said code works ??
You want $_post vars before you even submit the form? That's what code looks like. You don't call the form from another view/controller/method to submit right? It's just this login view.

wrap your code in a
Code:
if ($this->input->post('btnLogin'))
{
//all your code except the "Render the view" part
}

or do as InsiteFX said

and test.




Theme © iAndrew 2016 - Forum software by © MyBB