Welcome Guest, Not a member yet? Register   Sign In
sample code for creating modal-view-controller
#1

[eluser]Anju[/eluser]
example for creating a login fom in CodeIgniter
#2

[eluser]Anju[/eluser]
exmple for creating codeigniter login code
#3

[eluser]gcc.programmer[/eluser]
A basic set up would involve say two controllers: Login, and Main. Also, for this example you would autoload the session and input libraries.

Login Controller:
Code:
class Login extends Controller{
    
     function Login(){
    
          parent::Controller();
     }

     function index(){
    
          if ( $this->session->userdata('logged') == TRUE ){
               redirect('main/index');
          }
          
          $this->load->view('login');
     }

     function process(){

          $user = $this->input->post("user");
          $pass = $this->input->post("pass");

          if ( $user == 'foo' and $pass == 'bar' ){
              $data = array(
                              'user'     => $user,
                              'logged'     => TRUE
                             );

             $this->session->set_userdata($data);
            redirect('main/index');
            
           } else {
    
                   $this->session->set_flashdata('msg', 'User and/or Pass incorrect');
                redirect('login/index');
           }
     }

     function logout(){
          $this->session->sess_destroy();
           redirect('login/index');
     }
}

Login View:
Code:
<form action="login/process" method="POST"/>
<?= $this->session->flashdata('msg');
<p>User:&lt;input type="text" name="user"/&gt;&lt;/p>
<p>Pass:&lt;input type="password" name="pass"/&gt;&lt;/p>
<p>&lt;input type="submit" value="submit" name="submit"/&gt;&lt;/p>
&lt;/form&gt;

Main Controller:
Code:
class Main extends Controller{
    
    function Main(){

        parent::Controller();
    }
    
    function index(){

        if ( $this->session->userdata('logged') != TRUE ){
            redirect('login/index');
        }

        $this->load->view('main');
    }
}

Main View:
Code:
Stuff in main view
<a href="login/logout">Log Out</a>

Essentially what you're doing is this:

In our Login controller, we first check to see if the user is logged in, using the index function, which presumably would be the default controller ( Login ). If the user is already logged in, we redirect them to the main controller index page, which is the protected portion of the site. If they aren't we display the login form. If they don't give a correct username/pass, we redisplay the login form, with the error message.

In the Main controller, we're simply checking to see that the user is in fact logged in. If they are, they get to see the content, and if they're not, they get bounced back to the log-in page. The link on the main page is so the user can log out.

That is the most bare-bones kind of log-in with sessions that comes to me at the moment. Obviously, you'd likely want to ramp up to checking for the user in a database, etc. But if you don't know how to do a log-in yet, the database part adds a level of complexity you might not be ready for.

Play with this, maybe check for a bunch of different users in an array, or look them up from a file saved on disk, and when you get more comfortable with the framework, move on to the Database part.




Theme © iAndrew 2016 - Forum software by © MyBB