views:

31

answers:

1

Hello, im trying to make a login script in codeigniter, but I only got a blank page, I want it to say "username or password is wrong" and if the username and password is right it should set session to true, hope some one can help me

here is my code

<?php

class Login extends Controller {

    function index()
    {

        if ($this->_submit_validate() === FALSE) {
            $this->index();
            return;
        }


    }

    private function _submit_validate()
    {
        $this->form_validation->set_rules('username', 'Username',
            'trim|required|');

        $this->form_validation->set_rules('password', 'Password',
            'trim|required');

        return $this->form_validation->run();
    }


    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'Du har ikke adgang til denne side';   
            die();      
            //$this->load->view('login_form');
        }       
    }

}

here is my view file

        <h3>Medlem login</h3><br>
        <?php
        echo form_open('login/');
        echo validation_errors('<p class="error">');
        echo form_input('username', 'Username');
        echo form_password('password', 'Password');
        echo "<br />";
        echo form_submit('submit', 'login');
        echo anchor('signup', 'Opret Account');
        echo form_close();
        ?>
A: 

Your code is completely wrong and does not follow the RESTful pattern. For starters, your index function will cause your script to block:

   function index()
{

    if ($this->_submit_validate() === FALSE) {
        $this->index();
        return;
    }


}

Instead of calling the index function again on failure, you should load your view of the form. Then add an else clause in case validation succeeds. Like:

   function index()
{

    if ($this->_submit_validate() === FALSE) {
        $this->load->view('view_form');
    } else {
        $this->function_to_set_session();
    }
}

In the function_to_set_session(), you'd then do something along these lines:

function login(){
    // set session vars
    $this->session->set_flash('flash_message_to_show_after_reload'); // this is the message on succes
    redirect('path_to_redirect');
}

I figure you're just getting started with CodeIgniter? If so, I advise to follow a tutorial.

Anzeo
hm im new and i have follow some tutorials but all do it different :S,
Simon
but i want to do if the username or password is wrong it get a error "wrong username os password" so no loading a new view file
Simon
But to show anything on your screen you need to load a view! So in case of an error reload your form view and use the form_helper of CodeIgniter to show the errors. In case of success, set your session variables and redirect your user to the correct page. This is how your flow should work.
Anzeo