Auth Component's user function not working in case multiple routing - php

I have routed my site as admin moderator. Means When I use Auth component my sessions are created as Auth->Admin->id & Auth->Moderator->id And Auth->User->id this way.
Now I tried function $this->Auth->user its returning null why??
As I know $this->Auth->user returns session.
I know the traditional way of $this->Session->reads('Auth.Admin') and else but I want to use Auth->user function why its not working

Please use this to get user session data.
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function index() {
pr($this->Auth->User());
}

auth_helper:<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('auth'))
{
function auth()
{
$CI =& get_instance();
$CI->load->model('Auth_model');
return $CI->Auth_model;
}
function Adminauth($admin_id)
{
if($admin_id == false)
{
redirect('login');
}
}
}
auth_model:
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Auth_model extends CI_Model {
private $user = null;
private $ci = null;
public function __construct() {
parent::__construct();
$CI =& get_instance();
$this->ci = $CI;
}
public function get($name, $default = null){
if ($this->ci->session->has_userdata('auth.Adminuser_id')) {
$user_id = $this->ci->session->userdata('auth.Adminuser_id');
$this->user = $this->db->query("SELECT * FROM members WHERE u_id=? LIMIT 1", array($user_id))->row();
}
return !empty($this->user) ? $this->user->u_name : $default;
}
public function is_logged(){
return $this->ci->session->has_userdata('auth.Adminuser_id');
}
public function login($user_id){
return $this->ci->session->set_userdata('auth.Adminuser_id', $user_id);
}
public function logout(){
return $this->ci->session->unset_userdata('auth.Adminuser_id');
}
}

Related

How to used User agent in Codeigniter3.0.6?

I am testing with Codeigniter3.0.6 on User agent as below function then I try to testing IPhone, and Chrome inspect device mode but I got only number 1.
I want to check if I view this website in Mobile phone it will echo number 2
if in PC browser echo number1 if can't detect browser show 0.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Main_Controller extends MY_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
var_dump($this->CheckDevices());
exit();
$this->data['deviceType'] = $this->CheckDevices();
if ($this->data['deviceType'] == 1) {
$this->set_navigation();
}
elseif ($this->data['deviceType'] == 2) {
return false;
}elseif($this->data['deviceType'] == 0){
return false;
}
}
private function CheckDevices()
{
$this->load->library('user_agent');
$agent = '';
if ($this->agent->is_browser()) {
$agent = 1;
} elseif ($this->agent->is_mobile()) {
$agent = 2;
} else {
$agent = 0;
}
return $agent;
}
private function set_navigation()
{
$this->load->library('session');
$this->load->library("nav_libs");
return $this->data['menus'] = $this->nav_libs->navigation();
}
}
?>
you can try this
application/core/My_Core.php
class My_Core extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function detect_dvice(){
$this->load->library('user_agent');
if( $this->agent->is_mobile()){
$_is_mobile = 1;
}
else{
$_is_mobile = 2;
}
return $_is_mobile
}
}
normal controller
application/controllers/Test_controller.php
class Test_controller extends My_Core{
public function __construct(){
parent::__construct();
}
public function index(){
echo $this->detect_dvice();
}
}

Use controller from another controller CodeIgniter and HMVC

I want to load a function from another controller. This is my structure:
- modules
--orderpages
---controllers
----WebshopCore.php
----WebshopController.php
My function insertItemInCart in WebshopController.php is called. But when i want to execute a function from another controller it crashes.
class WebshopController extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->module('orderPages/WebshopCore');
}
function insertItemInCart(){
$partId = $this->input->post('partId');
$quantity = $this->input->post('quantity');
$output = $this->WebshopCore->getPickLocations($partId,$quantity);
}
}
My WebshopCore:
class WebshopCore extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function getPickLocations($partId,$amount){
$result = "test";
return $result;
}
}
What goes wrong? I don't get it
The solution:
$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity);
You should write a library in that case.
In application/libraries create Cart.php (or whatever you want)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function getPickLocations($partId, $qty)
{
//Your stuff
}
}
And then in your controllers :
$this->load->library("cart");
$data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity'));

How do I disable a constructor for only a particular method in Codeigniter?

I have more than 30 methods in this User Controller.I don't want to create another controller just for this single method.
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
if(empty($_SESSION['userid'])){
$error['error'][] = "Please LogIn";
echo json_encode($error);
exit;
}
}
public function index(){
}
public function get_public_pages(){
}
}
I don't want the constructor function to run when accessing get_public_pages method.How can I do it?
Try this,
public function __construct(){
parent::__construct();
$method = $this->router->fetch_method();
if(empty($method ) && $method != 'get_public_pages'){
if(empty($_SESSION['userid'])){
$error['error'][] = "Please LogIn";
echo json_encode($error);
exit;
}
}
}

Cannot exit in CodeIgniter constructor

I'm basically trying a session control. If the user is logged in, it's ok to move on. But if he's not logged in, then it will show a log-in screen and then die. However, when I use die or exit in the constructor, it does not show the log-in screen; it immediately dies. The code is as following:
private $username = null;
private $mongoid = null;
private $neoid = null;
public function __construct(){
parent::__construct();
// session to global
$this->username = $this->session->userdata( 'username');
$this->mongoid = $this->session->userdata( 'mongoid');
$this->neoid = $this->session->userdata( 'neoid');
// check if user is logged in
if( $this->username == "" || empty( $this->username)){
$this->load->view( 'access/login');
die;
}
}
It shows the log-in page if die is not written there, but with die, it does not show. Why do I want to use die? Because if I don't use, it moves on the index function and I don't want it to execute index function if the user is not logged in.
What is wrong here? What should I use to stop executing?
CodeIgniter does not instantly output the view when you load it with $this->load->view();, it puts the output of the view to the output buffer instead and after everything is loaded it flushes the whole output to the user. With that said, the problem you are seeing is that it buffers the output, then it dies without flushing it.
die is really bad and should not be used outside debugging, you should better use something like a variable switch. If it's only for the controllers scope, then you can make a
private $show_everything_else = true;
In the constructor:
if( $this->username == "" || empty( $this->username)){
$this->load->view( 'access/login');
$this->show_everything_else = false;
}
In any of the controller's methods:
if($this->show_everything_else) {
// ...
}
In any case, this solution is a quick fix and there are much better possibilities how to do this, but all of them require a different approach.
You can have a method and call it in constructor:
function __construct() {
parent::__construct();
$this->_is_logged_in();
}
and the method should look like this:
function _is_logged_in() {
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true) {
redirect('login');
die();
}
}
And, of course, you should have controller for login, which can look like this:
<?php
class Login extends CI_Controller {
function index() {
$this->load->view('LOGIN-VIEW');
}
function validate() {
$this->load->model('login_model');
$data = $this->login_model->validate();
if ($data != false) {
$data['is_logged_in'] = true;
$this->session->set_userdata($data);
redirect('MAIN-CONTROLLER-AFTER-LOGIN');
}
else {
$this->index();
}
}
function logout() {
$this->session->sess_destroy();
$this->index();
}
}
This what i posted, also preserve sessions in database.
Login model can be as primitive as this:
class Login_model extends CI_Model {
function validate() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1) {
$data = $query->row_array();
return $data;
} else {
return false;
}
}
The Controller class will automatically run the index() function. So, to make it work, you must return the index() if the login check in construct is failed. Here what you can do:
private $username = null;
private $mongoid = null;
private $neoid = null;
private $no_login = false;
public function __construct(){
parent::__construct();
// session to global
$this->username = $this->session->userdata( 'username');
$this->mongoid = $this->session->userdata( 'mongoid');
$this->neoid = $this->session->userdata( 'neoid');
// check if user is logged in
if( $this->username == "" || empty( $this->username)){
$this->load->view( 'access/login');
$this->no_login = true;
}
}
function index(){
if($this->no_login) return;
// another statement if login success.....
}

get an php fatal error in codeigniter controller

Here it is my code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database(); /* This function it's used to connect to database */
$this->load->model('User','user'); /* This call the model to retrieve data from db */
}
public function index()
{
if(!file_exists('application/views/_login.php'))
{
show_404();
}
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<h4 style="text-align:center;">','</h4>');
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','password','trim|required|xss_clean|callback_pass_check');
if($this->form_validation->run() == FALSE)
{
$data['title'] = "User Access";
$data['author'] = "Salvatore Mazzarino";
$this->load->view('templates/_header',$data);
$this->load->view('_login',$data);
$this->load->view('templates/_footer',$data);
}
else
{
redirect('home', 'refresh');
}
}
public function pass_check($pass)
{
$result = $this->user->find_user($this->input->post('username'),$pass);
if($result == 1)
{
$session_array = array();
foreach ($result as $row)
{
$session_array = array('id'=> $row->id,'username'=> $row->username); /* Create a session passing user data */
$this->session->set_userdata('logged_in',$session_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('pass_check',"Invalid username or password!</br>Try again, please!");
return FALSE;
}
}
}
Here it is the line
$this->session->set_userdata('logged_in',$session_array);
that cause the error:
PHP Fatal error: Call to a member function set_userdata() on a non-object
Someone could say me the reason?
Make sure the session library is loaded.
You can add it in application/config/autoload.php to be loaded automatically.
http://ellislab.com/codeigniter/user_guide/libraries/sessions.html

Categories