Begin MVC with CodeIgniter (PHP) – Sample Login

Source File : Click here to download (Locate the extracted folder inside wamp->www folder and run by localhost)

CodeIgniter is a simple yet powerful MVC(Model-View-Controller) framework for PHP. In this post I’m gonna share you a simple login app that is developed using this framework to give you a basic understanding on how to start developing with this. This example will focus on handling controllers to serve the user with different views as well as passing data from the views to controllers and validate/process them inside the controller using functions.

ci_ex01

01. First what you should do is setting up the base url inside application->config->config.php
(You need to give the server path that your project resides) In my case I have set my base url as follows,
$config[‘base_url’] = ‘http://localhost:8080/my_ci_app/’;
 
02. In the index function of welcome.php, inside application->controllers, you set the view that the user should see at the begining, In my case it is like,
public function index()
{
$this->load->view(‘login’);
}
03. When start this example application, it serves the Login view to the user, then the inputs for “Username” and “Password” will be transferred to validatelogin controller using its FORM action as follows,
<form action=”<?php echo base_url();?>index.php/validatelogin/validate” method=”post” name=”form1″>
<table>
<tr>
<td>Username</td><td><input name=”uname” type=”text” /></td>
</tr>
<tr>
<td>Password</td><td><input name=”upass” type=”password” /></td>
</tr>
<tr>
<td><input type=”submit” value=”Login”/></td>
</tr>
</table>
</form>
/*Note: To make base_url() work, you have to add <?php $this->load->helper(‘url’); ?> after body tags inside your view, that is the helper library for url’s*/
 
04. Inside my validatelogin controller I have written a function called “Validate”(not inside index function). That is the controller we are calling inside the action of FORM tag. validate function is like follows,public function validate(){ if($_POST[‘uname’]==”user”&&$_POST[‘upass’]==”123″){
$this->load->view(‘home’);
}else{
$this->load->view(‘error’);
}

05. If user entered “user” for username and “123” for password, it will redirect the user into home.php view, else error.php view.
Advertisements

One thought on “Begin MVC with CodeIgniter (PHP) – Sample Login

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s