In my MY_Controller.php I have a redirect in there if session has expired.
Then it redirects to the logout controller. I have added a if statement agent->is_referral to the logout controller and set flashdata. Because it has been redirect from dashboard controller it should pick up that if statement and set flashdata but does not.
Question: If the customer is redirected from whatever controller to logout then how to get is_referral() to detect and set the flashdata message. flashdata message is not setting when inside is_referral() if statement.
controllers > account > Logout.php
<?php
class Logout extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->library('user_agent');
if ($this->agent->is_referral()) {
// Does not set the flashdata if redirected from another controller.
$this->session->set_flashdata('warning', 'Your session has expired!');
}
// Flashdata works if have it here.
// $this->session->set_flashdata('warning', 'Your session has expired!');
$this->customer->logout();
redirect('/'); // redirect to login page.
}
}
core > MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if ($this->uri->segment(1)) {
if (!$this->session->userdata('customer_id')) {
$route = $this->uri->segment(1) .'/'. $this->uri->segment(2);
if (isset($route)) {
$ignore = array(
'account/logout'
);
if (!in_array($route, $ignore)) {
redirect('account/logout');
}
}
}
}
}
}
controllers > account > Dashboard.php
<?php
class Dashboard extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('common/header_view');
$this->load->view('account/dashboard_view');
$this->load->view('common/footer_view');
}
}
Related
After right login details,form redirects to same place
This is my controller for the login
The login should be redirected to the dashboard after validation and database check
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
if($this->session->userdata('logged_in')) {
redirect(base_url().'dashboard');
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
redirect(base_url().'dashboard');
}
}
$this->load->view('Templates/header', $template);
$this->load->view('Login/login_form');
$this->load->view('Templates/footer');
}
}
code for Dashboard
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('dashboard_model');
if(!$this->session->userdata('logged_in')) {
redirect(base_url());
}
}
Try This Method
redirect('dashboard');
Try site_url() as well. Make sure there are proper code(error free) of view in dashboard controller.
redirect(site_url()."/dashboard");
I don't know how to phrase this question on google so i couldn't find any answers.
In my Views folder, i have templates folder with header,navbar,footer inside.
Whenever i load a view from my controller i would have to do this,
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/pagename');
$this->load->view('template/footer');
How do i do this with redirect? I don't know why but whenever i see code snippets of successful logins or failures they always use the redirect function instead of load view like the above.
for example:
function __construct() {
parent::__construct();
if($this->ion_auth->logged_in()==FALSE)
{
redirect('pages/login');
}
}
or can i use this and will this still be acceptable?
function __construct() {
parent::__construct();
if($this->ion_auth->logged_in()==FALSE)
{
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/login');
$this->load->view('template/footer');
}
}
function __construct() {
parent::__construct();
if($this->ion_auth->logged_in()==FALSE)
{
redirect('controller/login');
}
}
in your controller, create a function called login
function login() {
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/login');
$this->load->view('template/footer');
}
In redirect you need to use controller/method_name
redirect('controllername');
or
redirect('controllername/method');
Rather than construct you can use remap. to redirect if the user is logged or not
REMAP
public function _remap($method, $params = array()){
if(method_exists($this, $method)){
if($this->ion_auth->logged_in()==FALSE){
return call_user_func_array(array($this, $method), $params); //home page
}
return call_user_func_array(array($this, 'login'), $params); //if not logged in
}
show_404();
}
LOGIN
public function login() {
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/login');
$this->load->view('template/footer');
}
I have created a web application with Codeigniter, and I have a problem at the url, controller and structure level.
I have the following web structure.
http://projectroot/admin
Then I have several sections like:
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
I'm working with sessions and other libraries
Currently I have everything in a single controller called Admin, but I would like to create independent controllers that would be calling each part of the url.
In Admin I have:
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
if($this->session->userdata('login')){
if($this->session->userdata('urlnow')){
$url = $this->session->userdata('urlnow');
redirect($url);
}else{
redirect('admin/index');
}
}else{
$data = array();
$data['usererror'] = $this->session->flashdata('usererror');
$data['passerror'] = $this->session->flashdata('passerror');
$data['message'] = $this->session->flashdata('message');
$this->load->view('admin/index', $data);
}
}
...
public function users() {
code....
}
public function profile() {
code....
}
public function section_tracking() {
code....
}
public function section_products() {
code....
}
My idea is that the controller folder contains something like this:
admin.php
users.php
profile.php
section_products.php
section_visits.php
Creating independent Admin extends classes (user, profile, section_tracking and section_products) as independent controllers outside of admin, with a structure similar to this:
users.php
class Users extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
profile.php
class Profile extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
How can I do it? I don't want to use HMVC, I just want with MVC native.
Thank you
In your application/controllers/admin folder...Make following files
Admin.php
Users.php
Profile.php
Section_products.php
Section_visits.php
Then make call to each files like this...
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
And here you want to extends admin controller.so make Admin_controller.php by extending CI_Controller in application/core. Your `Admin' controller must be like this....
class Admin_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Then always extends Admin_controller.CI_Controller automatically extended.
I am new to CodeIgniter framework. I am using 2.1.4 version. I designed a simple login form, with a javascript validation, and the home page of a site. Can you please help me to understand how to declare session , and how to destroy the session on clicking signout link.
controller file of login page ( to load the view page login.php ):-
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index(){
$this->load->view('login');
}
function success() {
redirect ('home');
}
}
The controller file home.php for the view home.php
class Home extends CI_Controller {
// local constructor will be overriding the one in the parent controller class
// for using a constructor in any of my Controllers
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
}
I have designed the view page home.php, and gave the signout link:-
<div class="logout">Signout</div>
For initializing the session, i need to know, what all constructor changes/ config changes need, and the method of session destoy.
To start session library, Go to application/config/config.php and change the below line:
$autoload['libraries'] = array('session');
It would be better if you start your session in the autoload.php. To destroy session you would use :
$this->session->sess_destroy();
To set session :
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
here is an controller... first of all u need to declare a session so that you have two choice to declare one is Go to application/config/config.php change the code as
$autoload['libraries'] = array('session');
and follow this following method (controller)
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
}
function index(){
$this->load->view('login');
}
function success() {
$user=$this->input->post('user');
$psw=$this->input->post('pswd');
$this->load->model('validation');
$result=$this->validation->useraccess($user,$psw);
if($result)
{
$this->session->set_userdata('username', $user); //setting session
redirect ('home');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect('login','refresh');
}
}
this is model where validation done
Class Validation extends CI_Model{
function __construct(){
parent::__construct();
}
function useraccess($user,$pswd)
{
$query = $this->db->query("select * from user where username='$user' AND password='$pswd'");
foreach ($query->result_array() as $row)
{
if($row['username']==$user AND $row['password']==$pswd)
{
return true;
}
else
{
return false;
}
}
}
}
here is a view
login page
create 2 text box and 1 submit button and declare form action as
localhost/index.php/login/success
for logut
localhost/index.php/login/logout
I have a controller, which maps to section of my site and all of the pages within it (methods) should only appear if the user is logged in. Otherwise they should be redirected back to a login screen.
To get it working I've just done this:
function index() {
if ($this->session->userdata('logged_in')) {
$this->load->view('main');
} else {
redirect('/login');
}
}
function archive() {
if ($this->session->userdata('logged_in')) {
and so on... repeating that check in each method. What's the simplest way of doing this check once for multiple-or-all methods in the controller?
You can run code in every method of a Controller by running it in the __construct() method:
function __construct()
{
parent::__construct();
if ( ! $this->session->userdata('logged_in'))
{
// Allow some methods?
$allowed = array(
'some_method_in_this_controller',
'other_method_in_this_controller',
);
if ( ! in_array($this->router->fetch_method(), $allowed)
{
redirect('login');
}
}
}
You can remove the "allowed" bits if you want to restrict access to the whole thing, but there are better ways to do this, like creating a base controller:
// Create file application/core/MY_Controller.php
class Auth_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
if ( ! $this->session->userdata('logged_in'))
{
redirect('login');
}
}
}
Then have your restricted controllers extend Auth_Controller instead of CI_Controller. Now your code will be run every time the controller is loaded.
More info on extending core classes: http://www.codeigniter.com/user_guide/general/core_classes.html#extending-core-class
Also of interest: http://php.net/manual/en/language.oop5.decon.php
For codeIgniter 3 I modified Wesley Murch's answer to this
// Create file application/core/MY_Controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$CI = & get_instance();
$CI->load->library('session');
$CI->load->helper('url');
if ( !$this->session->userdata('logged_in'))
{
redirect('login');
}
}
}
Then in any controller to check authorization I used
class News extends MY_Controller {
//code here
}
If you use modules and different sessions for website users and admin users, you can use this code to perfectly redirect them to different login pages-
function __construct() {
parent::__construct();
$CI = & get_instance();
$CI->load->library('session');
$CI->load->helper('url');
// echo "<pre>";print_r($this->router);echo "</pre>";
/**
* if webmaster then check admin session else check user session
* But there may be some classes's method that doesn't requires login hence it is also need to check if
* current request is for those methods before checking session
*/
//to use $this->config->item('webmaster_name') this you have to define
// $config['webmaster_name'] = "webmaster"; in config.php file
if ($this->router->module == $this->config->item('webmaster_name')) {
if (!$this->session->userdata('admin')['id']) {
redirect($this->config->item('webmaster_name').'/login');
}
} else {
if (!$this->session->userdata('user')['id']) {
redirect('login');
}
}
}
If you also want users to allow to access some methods from any particular controller without being logged in you can use this code -
function __construct() {
parent::__construct();
$CI = & get_instance();
$CI->load->library('session');
$CI->load->helper('url');
//echo "<pre>"; print_r($this->router);echo "</pre>"; //_pr($this->config->item('excluded_auth'));
/**
* if webmaster then check admin session else check user session
* But there may be some classes's method that doesn't requires login hence it is also need to check if
* current request is for those methods before checking session
*/
if ($this->router->module == $this->config->item('webmaster_name')) {
if (!$this->session->userdata('admin')['id']) {
redirect($this->config->item('webmaster_name') . '/login');
}
} else {
if (array_key_exists($this->router->class, $this->config->item('exclude_auth')) && in_array($this->router->method, $this->config->item('exclude_auth')[$this->router->class])) {
//echo "escape this method. don not validate for a session";
} else {
if (!$this->session->userdata('user')['id']) {
redirect('login');
}
}
}
}
Note: You can define a custom config file for defining your excluded methods like as-
//save file in application/config/without_auth_methods.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['exclude_auth']['news'] = array('index', 'view');
$config['exclude_auth']['users'] = array('index');
I use this function:
Then just call $this->isAuthorized from your controllers __construct.
It allows me to control what controllers are accessed and what methods are accessed too.
protected function isAuthorized()
{
switch ( strtolower( $this->router->class ) )
{
case 'pages':
$disallowLoggedOut = array( 'dashboard' );
$disallowLoggedIn = array( 'index' );
break;
case 'users':
$disallowLoggedOut = array( 'logout' );
$disallowLoggedIn = array( 'register', 'login' );
break;
}
if ( $this->session->userdata( 'loggedIn' ) )
{
if ( in_array( $this->router->method, $disallowLoggedIn ) )
{
redirect( 'pages/dashboard' );
}
}
else
{
if ( in_array( $this->router->method, $disallowLoggedOut ) )
{
redirect( 'pages/index' );
}
}
}
Best way to deal such issue is to create a custom helper that should be called in every method of controller class e.g
Go to application/helpers and create a file login_helper.php
Paste the following code in the helper
<?php
defined('BASEPATH') OR exit('no direct access');
function isLogin($sessionType)
{
if(empty($_SESSION[$sessionType]))
redirect(base_url('loginURL'));
}
?>
Now load this helper into Controller's constructor.
application/controllers/Access.php
this way
defined('BASEPATH') OR exit('access denied');
class Access Extends CI_Controller
{
funcrion __construct()
{
parent::__construct();
$this->load->helper('login');
}
function home()
{
isLogin();
$this->load->view('home_page);
}
}