Skip to main content

Codeigniter initial setup


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

Popular posts from this blog

SETUP REST API IN CI

1. Create Rest_controller.php inside controllers and paste code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); require APPPATH . '/libraries/API_Controller.php'; class Rest_controller extends API_Controller { public function __construct() { parent::__construct(); } public function index() { $this->api_return(             [ 'status' => true,                'result' => "Welcome to Testservices."             ],         200); } } ?> 2. Create api.php inside config and paste code : <?php defined('BASEPATH') OR exit('No direct script access allowed'); /**  * API Key Header Name  */ $config['api_key_header_name'] = 'X-API-KEY'; /**  * API Key GET Request Parameter Name  */ $config['api_key_get_name'] = 'key'; /**  * API Key POST Request Parameter Name ...

NGrok Setup

 https://dashboard.ngrok.com/get-started/setup 1. Unzip to install On Linux or Mac OS X you can unzip ngrok from a terminal with the following command. On Windows, just double click ngrok.zip to extract it. unzip /path/to/ngrok.zip 2. Connect your account Running this command will add your authtoken to the default ngrok.yml configuration file. This will grant you access to more features and longer session times. Running tunnels will be listed on the endpoints page of the dashboard. ngrok config add-authtoken 1woFn9zVqcI4VeGuSIiN2VtmnPa_ZXuAuF1AAPkqApr7WVsQ 3. Fire it up Read the documentation on how to use ngrok. Try it out by running it from the command line: ngrok help To start a HTTP tunnel forwarding to your local port 80, run this next: ngrok http 80

API ( service ) Image or Video Upload

## SAVE  VIDEO public function uploadmedia() { $target_path = "assets/uploads/"; $target_path = $target_path . basename($_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { $this->api_return( [ 'status' => true,    'result' => 'uploaded success' ], 200); } else{ $this->api_return( [ 'status' => false,    'result' => 'failed' ], 20); } } ## SAVE FILE IMAGE OR VIDEO public function savefile() { $filetype = $_FILES['file']['type']; if (strpos($filetype, 'image') !== false) { $type = 'image'; } if (strpos($filetype, 'video') !== false) { $type = 'video'; }         $filename = trim($_FILES['file']['name']); // $userid = trim($this->input->get('userid'));...