Welcome Guest, Not a member yet? Register   Sign In
Invisible pages, no output
#1

[eluser]richzilla[/eluser]
Hi all, ive been working through creating my own simple user auth system, and thanks to getting loads of help from here ive just about cracked it. Ive just come up against one final problem. Ill post my controller to demonstrate:

Code:
<?php
    class Site extends Controller {
    
        function index() {
            $this->load->view('site_view');
        }
        
        function login() {
            $this->form_validation->set_rules('username','User Name', 'required|xss_clean');
            $this->form_validation->set_rules('password','Password','required|xss_clean');
            
            $this->form_validation->set_error_delimiters('<div class="error">','</div>');
            
            $data = array (
            'userName' => $this->input->post('username'),
            'userPass' => $this->input->post('password')
            );
            
            if ($this->form_validation->run() == FALSE) {
                $this->index();
            }
            else {
                
                $this->db->where('userName',$data['userName']);
                $this->db->where('userPass',$data['userPass']);
                $query = $this->db->get('users');

                $result = $query->result();
                
                if (isset($result)) {
                    foreach($result as $row) :
                        $this->session->set_userdata(array('loggedIn' => TRUE, 'userId' => $row->id, 'userName' => $row->userName));
                        $this->secure();
                    endforeach;
                }
            ***    else {
                    $err = array (
                    'error' => "The entered credentials were incorrect"
                    );
                    $this->load->view('site_view', $err);
                }
            }
        
        }
        
        function secure() {
            $data = array (
            'uName' => $this->session->userdata('userName')
            );
            $this->load->view('secure_page', $data);
        }
        
        function logout() {
            $this->session->sess_destroy();
            $this->load->view('site_view');
        }
    
    }

everything works fine until i enter valid, but incorrect login details (ive starred the line where i think the problem is). The page returned is completely blank, nothing in the source either so there is just no output. it comes back with a 200 code in the header so its not a server problem. I wondered if anyone could point out the mistake?

thanks again for the help.
#2

[eluser]n0xie[/eluser]
First check if the if-statement actually does what you think it does:
Code:
if ($result)
    {
        echo 'yay';
    }
    else
    {
        echo 'boooo';
    }
#3

[eluser]richzilla[/eluser]
everything seems to be working, im getting all the expected responses.
#4

[eluser]Phil Sturgeon[/eluser]
Code:
$result = $query->result();
                
                if (isset($result)) {
                    foreach($result as $row) :
                        $this->session->set_userdata(array('loggedIn' => TRUE, 'userId' => $row->id, 'userName' => $row->userName));
                        $this->secure();
                    endforeach;
                }

That code is VERY strange. Why are you testing if a variable you have just set is set? Surely !empty() would be more accurate?

Even better you could use if($query->num_rows() > 0).

Also, why foreach the result if you only want one? Use $query->row() instead.

I'm not even going to mention the mixing of standard and alternative syntax :-p

Clean up the code and post the entire file and we'll help. Right now with so many oddities its hard for me to spot the cause.
#5

[eluser]Aken[/eluser]
Not sure exactly what the issue of nothing being displayed is, as it's impossible to tell because we have no idea what's in your views either. I'd really just keep trying different things, commenting code out until you narrow it down.

BTW, you shouldn't use isset() to check for a query result like that. An empty result will be an empty array, which isset() will return TRUE. Use count(), or check $query->num_rows().
#6

[eluser]richzilla[/eluser]
Thanks for the help guys. tidying the code up a bit really did the trick.




Theme © iAndrew 2016 - Forum software by © MyBB