Codeigniter + ION AUTH...Can't tell if logged in - php

I'm building a Codeigniter application with ION Auth, which is going relatively OK. I've set up the ION Auth framework following this guide http://www.rappasoft.com/tutorials/5-building-a-simple-codeigniter-application-using-ion-auth.html#.U0Q5ufldUrU and when I log in using the default username and password I'm redirect to my homepage. So far, so good.
Problem then is that when I access one of my protected pages (i.e. a controller which extends MY_Controller), I'm redirected to the login page again.
I'm quite new to this, so just looking for some pointers of where to look. It seems I'm being logged in, but I don't really know how to check for sure.
I've tried this line of code in my view, but I get a couple of errors: user_info is undefined & trying to get property of a non object.
<?php echo $user_info->username;?>
MY_Controller in application/core:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->ion_auth->logged_in()) {
redirect('auth/login');
} else {
//Store user in $data
$data->user_info = $this->ion_auth->user()->row();
//Load $the_user in all views
$this->load->vars($data);
}
}
}
My 'members only' area controller class declaration:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('grocery_CRUD');
}
//other functions omitted

Add this to your MY_Controller and see what is returned
var_dump($this->ion_auth->logged_in());exit;

Related

Code Igniter not running the method in the controller

I have a form in a view called login_view that I would like to target a function in the Login controller.
For my form in login_view I have echo form_open('/Login/validate_credentials');
In my Login.php controller I have:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function __construct()
{
parent::__construct();
}
public function validate_credentials(){
$this->load->view('welcome2');
}
}
?>
When I submit the form from the browser, instead of loading the welcome2 view like I expect, it tries to load the URL of:
localhost/project/Login/validate_credentials
and I just see the default page for WAMP.
It's my first day in Code Igniter so I'm sure the answer is simple.
Just replace your controller construct by this:
public function __construct()
{
parent::__construct();
$this->load->helper(array('url','text','html','form','string'));
}

CodeIgniter - Hyperlink creation

I have tried to create a hyperlink to another page using anchors however it doesn't seem to work.
My function is called calendar and its stored in calcontrol.php within a controllers directory.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}
?>
I have tried other methods but they do not seem to work.
edit : this is my a href
Link
Filename and classname must be the same, otherwise CI will not be able to load it. And if using CI 3, make sure your class name and filename are both Ucfirst. If you have everything set up like this, then you should be able to view the page through http://yourdomain.com/calendar/calendar
Try this code. You forgot to load url helper. File Calendar.php - nogice capital C if CI3:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}

Codeigniter -- working with multiple controllers

I have a question which regards to controllers. Let's get it started:
I have a main controller named "admin.php", it has a menu for company, user management, etc. Each item on the menu has separate PHP file to hold different kinds data [I seems to be lengthy to combine them all in one php.
So for this example:
I have 3 controllers: admin.php , company.php, usermanagement.php
What I want is, link the company and management controllers as a child of admin. So if enter the address on the browser, it may look: localhost/admin/company and localhost/admin/usermanagement
I configured the routes and it's good but when I enter "localhost/company" it loads the company page which i didn't want to. i wanted to link them all as a child of an admin page.
How would I achieve this?
by the way here's a snippet of my code:
admin.php - Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_admin');
}
}
Company - Same as the admin
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_company');
}
}
Thanks,
James
EDIT: I have tried adding functions on the admin.php like:
function company() {}
function usermanagement() {}
but I guess it wasn't that effective since it will include lots of functions later on as I try to migrate my native php codes into this MVC architecture framework.
If your issue is that you like the way routes work but don't want people to be able to visit index.php/company/ and rather prefer that they visit admin/company you can always do:
class Company extends CI_Controller {
public function __construct() {
parent::__construct();
if ( $this->uri->segment(1) != "admin" ) {
redirect('admin/company/'.$this->uri->segment(3));
}
}
...
Although bear in mind that you would probably need a more complete URL forming method than just adding $this->uri->segment(3) but the general pattern is there.
You can add more functions in your 'admin' controller, so, default page is:
public function index() {
$this->load->view('view_admin');
}
'Subpage' is:
public function company() {
$this->load->view('view_company');
}
etc, etc...

Add public variable to all CI_Controller's and anything that extends them?

Hi I'd like to add a public variable ($this->data[]) to all instances of CI_Controllers, that way I can store some base rules for outputting a page (css/js, etc) then have each controller append to this array to add its own requirements (more css/js). I have a core library with custom view functions that take those arrays and inject them into the head tag of the page template.
The options I've thought of;
Edit CI_Controller and add it there... guessing that's a bad idea.
Create a shell controller that extends CI_Controller, add the var to that, then have every other controller extend the shell controller.
Any other clever ways?
I've only been using CI for about a month and I've tried reading through the docs but I can't find any built in ways to do something like this? Has anyone encountered this before and if so how did you solve it?
Thanks!
[edit] Using PHP 5.3.x [/edit]
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
You can extend CI_Controller to have the functionality you want.
application/core/MY_Controller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function my_function()
{
return "Cool return from my_function";
}
}
controllers/welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index()
{
echo $this->my_function();//echo's "Cool return from my_function"
}
}
You simply define the functionality you want in MY_Controller. Then in your controllers, use extends MY_Controller instead of extends CI_Controller and you can call the functions anywhere inside those controllers.
I think what you want can be easily achieved using traits. Check here: PHP: Traits
More specifically -> Example #11 Defining Properties.
The only limitation is it's PHP 5.4+
You can create your own a base controller file to inherit basic page load methods from in \application\core\MY_Controller.php as such:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
protected function loadPage($path, $data = array())
{
$this->load->view('common/head'); // Assuming you will use a folder for page parts
$this->load->view($path, $data);
$this->load->view('common/foot');
}
}
Afterwards in your page controller you can call upon the same methods much more easily:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
public function index()
{
$data = array(); // get data from model
$this->loadPage('user/view', $data); // Assuming you will use folders for sets of views
}
public function edit()
{
$data = array(); // get data from model
$this->loadPage('user/edit', $data);
}
}

Codeigniter: session class in routes.php

How can I access the codeigniter session class inside routes.php?
I need that class to route all the request (except /login) to a certain controller, unless the user has admin privileges ($this->session->userdata('logged')).
All the route rules are working, I only need to access that class.
You are not typically able to access the Singleton ($this->) from the config & routes file because the classes are not loaded at that point
although there are a few workarounds to access the session, the better way would be to a MY_Controller & the _remap() function:
http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping
Here's some sample code that explains a little more about how they work:
http://www.codebyjeff.com/blog/2012/11/ci-_remap-function-the-friend-you-never-knew-you-had
Create this MY_Controllerand store it in application/core/, then have your other controllers extend it:
<?php if (! defined('BASEPATH')) exit('No direct script access');
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->_check_auth();
}
private function _check_auth(){
if(!$this->session->userdata('is_admin')){
$this->session->sess_destroy();
redirect('login');
}
}
}
Note: above code assumes you already have a user login system in place.

Categories