Redirect Best Practice

#1
[eluser]MercuryLime[/eluser]
I recently started using CI and I love it.

I am building a web app and I planned to have all of the complex stuff in one 'processes' controller. I would submit forms to it and have it redirect to the appropriate page after going through logic. However, I haven't been able to get validation to work.

Form code (in a view loaded by 'home' controller):
Code:
<font style="color: #f00;">&lt;?php echo $this->validation->error_string; ?&gt;</font>

&lt;?php
echo form_open('process/login');
echo 'User:' . form_input('username') . '<br />';
echo 'Pass:' . form_password('password') . '<br />';
echo form_submit('submit', 'Enter!');
echo form_close();
?&gt;

Process code (in 'processes' controller):
Code:
function Login()
{
    $rules['username'] = "required";
    $rules['password'] = "required";
    $this->validation->set_rules($rules);
    if ($this->validation->run() == FALSE)
    {
        // Return to main page, display errors
    }
    else
    {
        // Try to log in
        $query = $this->db->get_where('users', array('username' => $this->input->post('username'), 'password' => $this->input->post('password')));
        if ($query->num_rows() > 0)
        { $this->session->set_userdata(array('username'=>$this->input->post('username'), 'password' => $this->input->post('password')));
        }
        else {
        }
    }
    redirect('');
}

If I submit the form it gets sent to the process and redirected back without showing any errors.

My questions are:
1) What is the best practice for redirects? Should I try to avoid them whenever possible (does it create multiple instances of CI and take up processing power?)
2) Any suggestions for why this code isn't working? I'm pretty sure it has something to do with the redirects.

#2
[eluser]lynkyle[/eluser]
I'm gonna give this a shot...

Form code (in a view loaded by 'home' controller):
Code:
&lt;?=$this->session->flashdata('error_message')?&gt; //if user and password does not match in database
<font style="color: #f00;">&lt;?php echo $this->validation->error_string; ?&gt;</font>

&lt;?php
echo form_open('process/login');
echo 'User:' . form_input('username') . '<br />';
echo 'Pass:' . form_password('password') . '<br />';
echo form_submit('submit', 'Enter!');
echo form_close();
?&gt;

Process code (in 'processes' controller):
Code:
function Login()
{
    $rules['username'] = "required";
    $rules['password'] = "required";
    $this->validation->set_rules($rules);
    if ($this->validation->run() == FALSE)
    {
        // load your view with the form here again - validation error will be printed
    }
    else
    {
        // Try to log in
        $query = $this->db->get_where('users', array('username' => $this->input->post('username'), 'password' => $this->input->post('password')));
        if ($query->num_rows() > 0)
        { $this->session->set_userdata(array('username'=>$this->input->post('username'), 'password' => $this->input->post('password')));
        redirect('to/wherever/you/want', 'location');
        }
        else {
        $this->session->set_flashdata('error_message', 'Wrong username or password');
        redirect('home', 'location');
        }
    }
}

EDIT: Your redirect() function is outside of your if-else conditional that's why it returns... Also, try reading again the validation class of CI. I know it got me confused the first few times. Smile

#3
[eluser]MercuryLime[/eluser]
Thanks. I see what that does. The only problem is, I want to redirect to the regular address instead of staying in the process/login directory even if the validation is false. I have decided to just cut out a processes folder and have the form just submit to itself (and I got it working, so no more help needed.)

EDIT: Thanks for the help--I keep coming back to this and realizing how it works.

#4
[eluser]Michael Wales[/eluser]
Quote:Why are you using flashdata--can’t the validation class take care of everything?
After a redirect your Validation object will disappear - Flashdata will allow you to persist data across a new HTTP request.

But, the way you are headed is the "preferred" way. Of course, there are hundreds of ways to accomplish this, but we all agree having a form submit to itself is by far the easiest way.

#5
[eluser]makedjakila[/eluser]
Quote:But, the way you are headed is the “preferred” way. Of course, there are hundreds of ways to accomplish this, but we all agree having a form submit to itself is by far the easiest way.

I used to treat forms like Lynkyle... the way CI propose to treat form is smart but hit his own limit when it comes to redirection because validation object disappear in this case.

So I decided to extend the Validation Library :
Code:
class MY_Validation extends CI_Validation {
    
    var $custom_error_string ;
    
    function add_custom_error($data='', $message='')
    {
        if ( ! is_array($data))
        {
            if ($message == '')
                return;
                
            $data = array($data => $message);
        }
    
        foreach ($data as $key => $val)
        {
            $this->custom_error_string .= $this->_error_prefix.$val.$this->_error_suffix."\n";
            
            $error = $key.'_error';
            $this->$error = $this->_error_prefix.$val.$this->_error_suffix;
        }
    }
}

It allows me to force errors...

#6
[eluser]Shujin[/eluser]
Hope you don't mind me posting in an old thread...

But along the same lines, how would you go about showing a message before it redirects?
Maybe a sort of redirecting delay...

Thanks


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.