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

[eluser]InsiteFX[/eluser]
Try this
Code:
public function login()
    {      
        // 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())
        {
            // Get the user data
            $user_data = (object) array(
                'email' => $this->input->post('email'),
                'password' => $this->input->post('password')
            );

            $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);
    }

InsiteFX
#12

[eluser]ian[/eluser]
@InsiteFX,

Unfortunately, your suggestoin didn't work. Again, $this->form_validation->run() needs those email & password settings to be in that array to be able to validate the data in it.

@osci,

You're partially right. The code is posting to the same controller function that loads the view for the login page. You'll notice the very last line of the login() function loads the appropriate view.
Code:
$this->template->build('login', $this->data);
However, if the login credentials are correct, it creates a session for the user and redirects to the page they were on previously. However, if it can't do that (either because they're simply visiting the login page before trying to login or their login credentials can't be validated), they're shown the login page. So I don't think your suggestion to check that the login button is posted is necessary.

I uninstalled PHP 5.3 and installed PHP 5.2, but that doesn't seem to fix the problem, which I wasn't really expecting since I'm not using any special characters like the person in the forum post you provided.

Again, I posted the code because I was asked to, but I don't think the controller is the problem. It's possible that the view is giving me problems, since I use things like base_url(), but I haven't been able to find a definitive answer for why $_POST data is empty/null, except when caused by a redirect, which I'm not detecting.
#13

[eluser]osci[/eluser]
The first time the method login is called it has no post values if you are talking about before submitting.

If I am redirected from another controller because / method because I was denied wouldn't I have empty values also?

Ian I read your code once more and have other issues to point out.

The way you code you make a $user->data array and then pass it to the view. Why's that? For passing values if validation process fails? If so for a start you should do in your array constructor
Code:
'email' => ($this->input->post('email')? $this->input->post('email') : '';
because input->post() returns false if the var is not set.

If I'm redirected from another site you would create a session var for a 404 page maybe?

Code:
if ($this->form_validation->run() or $this->ion_auth->logged_in())
Why is that combined?
Put the check for logged_in in the start of your login function.
So no need for the validation code (validation). You'll put your redirect code in the if logged_in and redirect accordingly.

Why not use your view without passing that array and use set_value from form_helper?
You would just have
Code:
<input type="text" name="email" maxlength="120" value="<?php echo set_value('email'); ?>" />
//where email is the name of the input field
and eliminate the need of your user_data array.

Also as I proposed you would put all (EDIT:almost) your code except the view rendering part in
Code:
if ($this->input->post('btnLogin'))
{
//all your code except the "Render the view" part
}

so your code would be like
Code:
if ($this->ion_auth->logged_in())
{
   // do your redirects
   // redirectto is set by another method or controller where access is denied
   // so you will not manipulate this session

   $redirectto = (strlen($this->session->userdata('redirect_to'))>0) ?
           $this->session->userdata('redirect_to') :
           '/'; //Your default redirect here
   $this->session->unset_userdata('redirect_to');
  
   redirect($redirect_to);
}

if ($this->form_validation->run())
{
   //check for validation with ion auth
   //Dont know but something like
   if ($this->ion_auth->checklogin($this->input->post('email'),
               $this->input->post('password')))
   {
      //same code as above
      //EDIT set your required session vars here
      $this->session->set_userdata('name', $this->input->post('email'));
      //now redirect
      $redirectto = (strlen($this->session->userdata('redirect_to'))>0) ?
           $this->session->userdata('redirect_to') :
           '/'; //Your default redirect here
      $this->session->unset_userdata('redirect_to');
  
      redirect($redirect_to);
   }
}

//since we are here either we have failed in validation or we just arrived
//show view
$this->data->sBodyClass = 'one-col';
$this->template->build('login', $this->data);

I didn't take into account your hook since I don't know what it's doing but I guess it's not a problem.


EDIT:
I noticed you do form_open('/users/login');
remove the first slash.
#14

[eluser]ian[/eluser]
@osci,

I understand that there won't be POST values before submitting; I'm concerned with the values I'm not getting AFTER posting. I believe a redirect would clear POST values, yes, but there isn't a redirect until AFTER a successful login.

Also, I appreciate you taking the time to give coding tips, but I'm not really worried right now about form/efficiency (I didn't even write this code, but I am trying to build on it). I'm not denying that it could use some refactoring. Not to overstate it, but this code works on a production server, which is why I believe it's related either to subtleties in my MAMP setup or a URL path that might act oddly on a server configured for a localhost virtual host.

Regarding your edit about form_open(’/users/login’); ... double-check it. I am using form_open(’users/login’); without the slash at the beginning.

Quote:Why not use your view without passing that array and use set_value from form_helper?
I don't understand how this would help my problem, but I'm also not sure how form_helper works. There might be a better way than to use the $user_data array, but I just don't understand why I get blank value when I print the value of a posted variable, for example,
Code:
log_message('error', $this->input->post('email'));
at the beginning of my login() function in my controller after doing a post.
#15

[eluser]osci[/eluser]
Take the time and setup a fresh install, no dependencies, make a form, and submit. Do you get $_POST?

I don't know if I 'll be of much help anymore, since I don't own a mac.
#16

[eluser]ian[/eluser]
So here's what I've done since the last post. I setup a fresh install of CI 1.7 (what my site is using). $_POST/$this->input->post() seems to be working normally. I didn't use my exact code to test the form, but I did notice something odd about my code that I failed to mention earlier since I just realized now it's weirder than I initially thought.

I've read about people doing a var_dump($_POST) and getting an empty array printout (something like "array(0) {}"). However, before & after posting, I get "NULL" as my printout for the var_dump($_POST). This seems really odd to me, as if $_POST is completely unavailable (i.e. NULL) rather than simply empty. In my fresh 1.7 install, I can do var_dump($_POST) and get expected results (all my posted vars are printed), so it doesn't seem like an issue where $_POST is reset by CodeIgniter. BTW, I'm using PHP 5.2, so $_POST should be available. Does this spur any ideas?
#17

[eluser]toopay[/eluser]
In your config file :
Code:
// Change AUTO to PATH_INFO
$config['uri_protocol']    = 'PATH_INFO';
#18

[eluser]ian[/eluser]
I had tried using 'PATH_INFO' for my uri_protocol before, but I tried it again and it still didn't work for me. Can you justify why you think that would help considering my problem? A little explanation would be helpful.
#19

[eluser]toopay[/eluser]
Because at some server sometime you need that to get the "$_GET" param properly, which i misread (you actually find for $_POST).

Related with your trouble, can you test to change your htaccess to this :
Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Or if last line generates 500 error, use these one instead :
# RewriteRule ^(.*)$ index.php?/$1 [L]
Because if your $_POST always returned null, whatsoever it is, i just expect some 'redirect' happens. But you still may need to set the 'PATH_INFO', just to remove the index.php from your uri, and ensure your $_POST send to the right controller(not the default ones).
#20

[eluser]toopay[/eluser]
In addition, you may want to take a look at my Proxy Lib and use it for perform a HTTP POST, when you test that. Just for easy debugging, instead to fill the form and press the submit button.




Theme © iAndrew 2016 - Forum software by © MyBB