I have the following code in my home_controller:
<?php
class HomeController extends AppController
{
var $name = 'Home';
var $uses = array();
function index ()
{
$this->set('title_for_layout', 'Welcome to CreatHive');
}
}
?>
Unlike normal authentication whereby you would redirect the user to a login form if they try and access certain actions or controllers I just want to show a different view for my index method on my homecontroller. So if the user is NOT logged in then show splash.ctp and if they are logged in then show index.ctp
Thanks
public function index() {
$this->set('title_for_layout', 'Welcome to CreatHive');
if (!$this->Auth->user()) {
$this->render('splash');
}
}
Related
In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}
In codeIgniter I have created routes to remove the controller/methodname from the url so but the problem is that when I try to access the second controller index method it is not loading it's loading the home controller
I have 2 controller Home.php and Admin.php when i type in localhost/foldername/ it is opeing home conrtollers index method but when I type in localhost/foldername/admin it is redirecting to home controller's second method which is page can any one help me out in writing the routes.
Here is my routes which I have created
$route['default_controller'] = 'home';
$route['([^/]+)/?'] = 'home/page/$1'; //If I comment this it is working properly for me
Admin Controller Admin.php
class admin extends CI_Controller {
public function index() {
if($this->session->userdata('is_logged_in') == true) {
$this->load->view('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
}
Home Controller Home.php
class home extends CI_Controller {
public function index() {
$front_page = $this->get_data->front_page();
$page_data = $this->get_data->AllData('pages', $front_page);
$data['title'] = $page_data->row()->pagetitle;
$class = explode("/", $page_data->row()->template);
$data['body_class'] = $class[1];
$this->load->view('includes/header.php', $data);
if($class[1] == 'home') {
$this->load->view('templates/slider');
}
$this->load->view('templates/navigation.php');
$page_content = $page_data->row()->template;
$this->load->view($page_content, $data);
$this->load->view('templates/footer-form.php');
$this->load->view('includes/footer.php');
}
public function page($id) {
$page_data = $this->get_data->AllData('pages', $id);
$data['title'] = $page_data->row()->pagetitle;
$class = explode("/", $page_data->row()->template);
$data['body_class'] = $class[1];
$this->load->view('includes/header.php', $data);
if($class[1] == 'home') {
$this->load->view('templates/slider');
}
$this->load->view('templates/navigation.php');
if($class[1] == 'home') {
$data['slider'] = 'templates/slider';
}
$data['content'] = $page_data->row()->template;
$this->load->view('index', $data);
$this->load->view('templates/footer-form.php');
$this->load->view('includes/footer.php');
}
}
Now can anyone help me out to solve this issue one thing more when I comment the custom routes it is working perfectly but the home controller for the page which i tried to remove method and controller name it is coming as 404 not found
According to what i undertand, Set your default controller, to check your authentication first:
$route['default_controller'] = 'admin';
//so that you can check weather the user is logged in or not.
To access your second controller's index function:
$route['Home'] = "home";
To access your second controller's page($id) function:
$route['Home/Page/(:num)'] = "home/page/$1";
// where num id the ID you will be passing to the page function.
How can i call the different function based on the login. I have a user login it should check whether the user is employee or student stored on the database on user_type.
I have a controller with two functions:
class mainextends CI_Controller {
function index()
{
echo "main";
}
function student($user_id)
{
//code
}
function employee($user_id)
{
//code
}
routes.php:
$route['default_controller'] = "main";
$route['people/(.*)'] = "main/student/$1";
$route['trainer/(.*)'] = "main/employee/$1";
$route['404_override'] = '';
Where can i check the user_type before redirecting it to appropriate url.
A simple solution is to use the codeigniter _remap function, just place it in your controller with some logic:
function _remap($methode){
// do something
}
A better solution is to have a basecontroller, which you include in every controller and expend it. In this basecontroller you put the access-control and the complete basic stuff.
I have a callback function that checks my login details are correct - If they are wrong it returns an error (this is working fine). If the details are correct it should set the session $this->session->set_userdata('logged_in',TRUE); and then continue with the function login and be redirected to the dashboard - This redirect works fine.
In my function index(){} on any dashboard pages have the line
if($this->session->userdata('logged_in')) redirect('dashboard/home');
The line above is the one that is causing my 310 redirect but I am unsure why?
I am wanting to check if the user is logged in redirect to dashboard/home else go back to the login page home/login
Controller:
class Home extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
//if($this->session->userdata('logged_in')) redirect('dashboard/home');
$data['contentMangement'] = $this->options_model->systemOptions();
$data['pageTitle'] = 'Login';
$data['message'] = "";
$this->load->view('_assets/header', $data);
$this->load->view('login', $data);
$this->load->view('_assets/footer');
}
public function login() {
$this->form_validation->set_rules('userEmail','Username', 'required|valid_email|trim|max_length[99]|xss_clean');
$this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[200]|xss_clean|callback__checkUsernamePassword');
if($this->form_validation->run() === FALSE) {
$data['contentMangement'] = $this->options_model->systemOptions();
$data['pageTitle'] = 'Login';
$data['message'] = validation_errors('<div class="alert alert-error">', '</div>');
$this->load->view('_assets/header', $data);
$this->load->view('login', $data);
$this->load->view('_assets/footer');
}elseif($this->form_validation->run() === TRUE){
redirect('dashboard/home');
}
}
function _checkUsernamePassword() {
$username = $this->input->post('userEmail');
$password = $this->input->post('userPassword');
$user = $this->user_model->check_login($username,$password);
if(! $user)
{
$this->form_validation->set_message('_checkUsernamePassword', 'Sorry the details you provided have not been found');
return FALSE;
}else{
$this->session->set_userdata('logged_in',TRUE);
return TRUE;
}
}
}
Here's what's happening.
Assume, I login correctly, in your login controller, you set logged_in = TRUE, and redirect me to dashboard/home. In the index() function at dashboard/home, if logged_in = TRUE (which it is) you redirect me to dashboard/home again. Once again, you check logged_in = TRUE, and redirect me again, and so on and so forth.
This is causing an infinite redirection loop which causes the 310 Error (Too many redirects).
You'll need to rework your login check. In index() in dashboard/home, do this:
if ($this->session->userdata('logged_in') === FALSE) redirect(site_url('dashboard/login'));
Now, when I visit dashboard home, you only redirect me away if I'm not logged in. This protects your dashboard home against non-authenticated users, while not throwing authenticated users into an infinite loop.
I think you first should create a MY_Controller to do that, what if one day you decide to change your session variable name from logged_in to logged? Will you open all your controllers and change its sessions?
Creating a MY_Controller you will make all other controllers, like Home_Controller extend the MY_Controller, like:
class Home extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
Your MY_Controller:
//under /application/core/
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
if(!$this->session->userdata('logged_in'))
redirect('login');
}
}
Of course that on your Login_Controller you will extend Controller and not MY_Controller or you'll be on a infinite loop.
Hey! I'm very new to Codeigniter, I'm trying to protect the entire admin controller. I figured I'd start here:
function Admin()
{
parent::Controller();
if(!isset($_SESSION['loggedin'])){
$this->login();
}
}
but this is obviously incomplete. How do I also stop the method that is trying to run ( ie index() ), and am I on the right track here??
Thanks for your help!!
there is
Extend the base controllers:
MY_Controller.php
<?php
class MY_Controller extends Controller {
function __construct()
{
parent::Controller();
$user_id = $this->session->userdata('user_id');
$this->data['user'] = $this->user_lib->get($user_id);
}
}
?>
you can store all kinds of info in this construct. This just gets the currently logged in users ID and assigns it the $data['user'] . This will be adjusted depending on which sort of auth library you use but you get the gist. You now have access to the current users ID, and all their details, from within any controller that extends "MY_Controller"
now you can create an "admin" controller, or any number of other ones to restrict access. like so:
Admin_Controller.php
<?php
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::Controller();
if($this->data['user']['group'] !== 'admin')
{
show_error('Error - you need to be an admin.');
}
}
}
?>
Public_controller.php
<?php
class Public_Controller extends MY_Controller {
function __construct()
{
parent::Controller();
if($this->data['user']['group'] !== 'member')
{
show_error('You need to login to see this page...');
}
}
}
?>
as you can see..possibilities are endless
So, for admin only pages - use the admin controller
for member only pages - public
for "normal" pages - use the default controller.
I'll link to Phil Sturgeon's article as it's where I read about it first
put the checking session code in every function in Admin Controller that you want to protect.
that is the easiest way to do it..