Welcome Guest, Not a member yet? Register   Sign In
Login System, help?
#1

[eluser]Declan[/eluser]
Hello CI forums,

I've been looking at a lot into codeigniter lately and recently decided to build something with it. I started with the login system, and I'm having trouble. Here's my code:

Login Controller:
Code:
<?

class Login extends Controller{
    
    function index()
    {
        $this->load->view('login');
    }
    
    function validate_login()
    {
        
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();    
        
        if ($query)
        {
            $data['username']         = $this->input->post('username');
            $data['is_logged_in']     = true;            
            $this->session->set_userdata($data);
            
            redirect('navigate/online');
        }
        else
        {
            $this->index();
        }
        
        
    }
}

Login Model:
Code:
<?

class Membership_model extends Model{
    
    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', sha1($this->input->post('password')));
        $query = $this->db->get('users');
        
        if ($query->num_rows == 1)
        {
            return true;
        }
    }

}

?>

Login View:
Code:
<?
    
    echo form_open('login/validate_login');
    echo form_input('username', 'Username');
    echo form_password('password', 'Password');
    echo form_submit('submit', 'Login');


?>


My problem is that when I click login it takes me to "login/validate_login", but gives me a 404.
All help is greatly appreciated.
:cheese:

Thanks, Declan.
#2

[eluser]danmontgomery[/eluser]
Code:
$this->load-model('membership_model');

Fixed this and your code works fine for me... Do you have some routing defined?
#3

[eluser]Declan[/eluser]
Thanks for the quick reply, I fixed that.. But I'm still getting the 404.
I'm not sure what you mean by do I have some routing defined.

Code:
$route['default_controller'] = "navigate";

That's for navigating through my template. I don't think that's the problem though because that works now and before I started the login system.
#4

[eluser]Declan[/eluser]
I searched "404" on my userguide and found:

Code:
"Modified method calling to controllers to show a 404 when a private or protected method is accessed via a URL"

This might be the problem?
#5

[eluser]stef25[/eluser]
What you saw in the userguide should not be the issue since the validate_login() method should not be protected or private (that would happen if you start the function name with an _underscore)

If you put a die('here'); as the first statement in your validate_login() and then visit your site.com/validate_login - do you still see the 404 (not sure if you removed index.php)?
#6

[eluser]Declan[/eluser]
I changed the validate_login() function to look like this:
Code:
function validate_login()
    {
        die('here');
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();    
        
        if ($query)
        {
            $data['username']         = $this->input->post('username');
            $data['is_logged_in']     = true;            
            $this->session->set_userdata($data);
            
            redirect('navigate/online');
        }
        else
        {
            $this->index();
        }
        
        
    }

But still get a 404, I'm boggled.
#7

[eluser]stef25[/eluser]
This means you are not even reaching this function. What do your routing and htaccess files look like?
#8

[eluser]Declan[/eluser]
Okay, I fixed some things and now with the die('here') added it dies.

Also, I'm using a sort of template system to navigate my site which might be affecting things?
I have one main view that loads other views inside of it, so the links on my website are 'game/tos' ect.

Code:
<?php

class Game extends Controller {
    
function index(){       $data['main_view'] = 'login';        $this->load->view('game.php', $data);    }    
function tos(){        $data['main_view'] = 'tos';        $this->load->view('game.php', $data);    }    
function faq(){       $data['main_view'] = 'faq';        $this->load->view('game.php', $data);    }
#9

[eluser]Declan[/eluser]
The problem was to do with my config, I had it so it was 'http://localhost/game/application', once I removed the application it worked...

Sorry for the waste of time here, I'll read up more on everything before I post any problems again.




Theme © iAndrew 2016 - Forum software by © MyBB