CodeIgniter Forums
Redirect Best Practice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Redirect Best Practice (/showthread.php?tid=8940)



Redirect Best Practice - El Forum - 06-05-2008

[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.


Redirect Best Practice - El Forum - 06-05-2008

[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


Redirect Best Practice - El Forum - 06-05-2008

[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.


Redirect Best Practice - El Forum - 06-06-2008

[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.


Redirect Best Practice - El Forum - 07-09-2008

[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...


Redirect Best Practice - El Forum - 08-15-2010

[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