Login Code With Codeigniter
Configure autoload.php
Open autoload.php from …xampp/htdocs/mysite/application/config/ and edit the following code as shown as below:
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
Configure config.php
Open config.php from …xampp/htdocs/mysite/application/config/ and edit the following code as shown as below:
$config['base_url'] = 'http://localhost/mysite/index.php/';
$config['index_page'] = '';
Configure database.php
Open database.php from …xampp/htdocs/mysite/application/config/ and edit the following code as shown as below:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'mysite';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Code for controller: file name: login.php directory: …xampp/htdocs/mysite/application/controllers/
class Login extends CI_Controller
{
function __construct() { parent::__construct(); }
public function index()
{
if (isset($_GET["login_attempt"])) {
$error = "Wrong User Name Or Password !!! ";
$this -> load -> view ('login_view', array('error' => $error)); }
else {
$error = "";
$this -> load -> view ('login_view', array('error' => '' )); }
if(isset($_POST['login'])&&$_POST['login']=='Login') {
$email = $_POST['email'];
$password = $_POST['password'];
$this -> load -> model('Model_login');
$user = $this -> Model_login -> login($email,$password);
}
} // End Of function index
} // End Of class Login
Code for model: file name: model_login.php directory: …xampp/htdocs/mysite/application/models/
class Model_login extends CI_Model
{
function Model_login() {
parent::__construct(); }
function login($email,$password)
{
$this -> db -> select('profile_id, email, password');
$this -> db -> from('profile');
$this -> db -> where('email = '. "'" . $email. "'");
$this -> db -> where('password ='. "'" . $password. "'");
$this -> db -> limit(1);
$query = $this -> db -> get();
$row = $query -> row_array();
if($query -> num_rows() ==1)
{
redirect ('welcome');
}
else
{
header('Location: '.config_item('base_url').'login'.'?login_attempt='.urlencode("1"));
}
}
}
?>
Code for view: file name: login_view.php directory: …xampp/htdocs/mysite/application/views/
Email Id:
Password:
Now Open your favorite browser and type http://localhost/mysite/index.php/login and that’s our login code.
Comments
Post a Comment