Welcome Guest, Not a member yet? Register   Sign In
displaying errors
#1

[eluser]weetstraw[/eluser]
I recently started playing with displaying errors if a URI segment is true. So if something is false, it redirects to the error page. Is this a bad practice? I personally like it.

Code:
function login()
{
    $error=$this->uri->segment(3);  
    $data['title']=" - Login";
    $data['body']="Currently we are accepting users ....";

    if($error==TRUE){
        $data['header']="Login - <span class=\"error\">Wrong Username or Password</span>";
    } else {
        $data['header']="Login";
    }

more code....

Code:
...code...

$this->session->set_userdata('name',$this->input->post('username'));
    redirect('');
} else {
    redirect('user/login/error');
}
#2

[eluser]TheFuzzy0ne[/eluser]
That doesn't make sense to me at all. Why would a segment be equal to boolean true? Surely that code doesn't work.
#3

[eluser]weetstraw[/eluser]
Sorry, I didn't include this in the original post. Here is the controller for the the action of logging in:

Code:
function loginAction()
{
    $rules['username']="trim|required";
    $rules['password']="trim|required|md5";
    
    $this->validation->set_rules($rules);
        
    if($this->validation->run() == FALSE){
        redirect('user/login/error');
    } else {
        if($this->User_model->checkUserLogin($this->input->post('username'),$this->input->post('password')) == TRUE){
            $this->session->set_userdata('logged_in',TRUE);
            
            $this->session->set_userdata('name',$this->input->post('username'));

            redirect('');
        } else {
            redirect('user/login/error');
        }
    }
}
#4

[eluser]TheFuzzy0ne[/eluser]
Ah, OK. I think I see where you're coming from. You just want to check if the third parameter is there in case of an error. In that case, I'd suggest that you check the third segment like this:

Code:
if ($this->uri->rsegment(3) == 'error')
{
    # ...

Bear in mind that most people would expect an error message explaining what went wrong rather than just seeing an error page. You can use flash data for this, and then you can check for the flash data key rather than checking the segment.

Also (and this is just my own preference here), I'd implement loginAction() like this:

Code:
function loginAction()
{
    $rules['username']="trim|required";
    $rules['password']="trim|required|md5";
    
    $this->validation->set_rules($rules);
        
    if( ! $this->validation->run() || ! $this->User_model->checkUserLogin($this->input->post('username'),$this->input->post('password'))
    {
        redirect('user/login/error');
    }
    else
    {
        $this->session->set_userdata('logged_in',TRUE);
        $this->session->set_userdata('name',$this->input->post('username'));

        redirect('');
    }
}

Functionally it's identical, but it's a little less code and I think a little easier to read IMHO.
#5

[eluser]weetstraw[/eluser]
Great! Thanks for the advice.




Theme © iAndrew 2016 - Forum software by © MyBB