1. Download Latest version of codeignitor from "https://codeigniter.com/".
OR
Download from link below :
https://drive.google.com/open?id=1K_VZEu2idorUS3vxHiIB5bIzIYt289kH
2. Extract .zip folder and rename it by project name.
3. Place that unziped folder into localhost htdocs folder.
4. Open project in browser as "localhost/projectname", Page with "Welcome to Codeignitor" will appears.
5. Open config.php inside application > config > config.php and set base URL "http://localhost/projectname".
6. Open database.php inside application > config > database.php and set hostname,username,password,database.
7. Create database and tables using localhost/phpmyadmin if not created.
8. Go to application > controller and create new file "Auth.php" as well as go to application > model and create new file
Auth_model.php
9. Write this code inside Auth_model.php
class Auth_model extends CI_Model
{
public function __construct()
{
parent:: __construct();
}
}
10. Write this code inside application > controller > Auth.php
class Auth extends CI_Controller
{
public function login()
{
echo 'login page';
}
}
hit the url=> "localhost/__projectname__/index.php/auth/login" login page able to see.//please configure projectname correctly
11. Create new "assets" folder inside root directory and place all css, js, fonts inside it.
12. Now Go to application > views and create login.php as well as register.php.
13. On register.php change link for css and js as :
<link href="<?php echo base_url();?>assets/css/bootstrap.min.css" rel="stylesheet">
<script src="<?php echo base_url();?>assets/js/bootstrap.min.js" ></script>
Design registration form with
<?php if(isset($_SESSION['success'])) { ?>
<div class="alert alert-success"><?php echo $_SESSION['success'];?></div>
<?php } ?>
<?php echo validation_errors('<div class="alert alert-danger">','</div>'); ?> //write wherever want to display errors
<form action="" method="POST">--[Registration feilds]--<input type="submit" name="register">--</form>.
14. Open application > config > autoload.php and set :
$autoload['libraries']=array('database','session','form_validation');
$autoload['helper'] = array('html','url','form');
15. For Registration open application > controller > Auth.php and add new register function coded as :
public function register()
{
if(isset($_POST['register']))
{
$this->form_validation->set_rules('username','Username','required');//Repeat for every feild that needs to be validate.
$this->form_validation->set_rules('password','Confirm Password','required|min_length[5]|matches[password]');
if($this->form_validation->run() == TRUE)
{
echo "form validated";
//add user in database
$data = array(
'table_feildname1'=>$_POST['name1_post_by_form'];
'table_feildname1'=>$_POST['name1_post_by_form'];
'username'=>$_POST['username'];
'username'=>md5($_POST['password']);
'created_date'=>date('Y-m-d');
);
$this->db->insert('table_name',$data);
$this->session->set_flashdata("success","registered, can login now");
redirect("auth/register", "refresh");
}
}
//load view
$this->load->view('register');
}
16. Design Login form
<form action="" method="POST">--[Login feilds]--<input type="submit" name="login">--</form>.
17. For Login open application > controller > Auth.php and edit login function coded as :
public function login()
{
$this->form_validation->set_rules('username','Username','required');//Repeat for every feild that needs to be validate.
$this->form_validation->set_rules('password','Password','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
$username = $_POST['username'];
$password = md5($_POST['password']);
//check user in database
$this->db->select('*');
$this->db->from('table_name');
$this->db->where(array('username'=>$username, 'password'=>$password));
$query = $this->db->get();
$user = $query->row();
//if user exists
if($user->email)
{
//temporary message
$this->session->set_flashdata("success","logged in");
$_SESSION['user_logged']=TRUE;
$_SESSION['username'] = $user->username;
//redirect to profile page
redirect("user/profile","refresh");
} else {
$this->session->set_flashdata("error","account doen't exists");
redirect("auth/login","refresh");
}
}
$this->load->view('login');
}
18. Go to application > controller create new controller User.php and code as below:
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
if(!isset($_SESSION['user_logged']))//if(isset($_SESSION['user_logged'])==FALSE)
{
$this->session->set_flashdata("error","Please login to view this page");
redirect("auth/login");
}
}
public function profile()
{
$this->load->view('profile');
}
}
19. Go to application > views and add new file "profile.php".
<h1>Profile page </h1>
<?php if(isset($_SESSION['success'])) { ?>
<div class="alert alert-success"><?php echo $_SESSION['success'];?></div>
<?php } ?>
<a href="<?php echo base_url()?>index.php/auth/logout">Logout</a>
20. Go to application > controller > Auth.php and add logout function as coded:
public function logout()
{
unset($_SESSION);
session_destroy();
redirect("auth/login", "refresh");
}
Comments
Post a Comment