8

Where can I place my "global" function, which will check, if user is logged in?

Because I want to do something like: user is able to browse some pages only when the function isLogged() returns TRUE, and I'd have to use it in some views, that's why it should be a "global" function, which I can access from anywhere.

Is that possible? Or there is any better solution for this?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Cyclone
  • 14,839
  • 23
  • 82
  • 114

3 Answers3

10

You should probably put it into a Library, and autoload the library. When you need to use the "logged_in" flag in a view, pass it in from the controller. Example:


application/libraries/Auth.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Auth
{

    public function is_logged_in ()
    {
        // Change this to your actual "am I logged in?" logic
        return $_SESSION['logged_in'];
    }

}

application/config/autoload.php

...
$autoload['libraries'] = array(
    ...
    'auth',
    ...
}

`application/controllers/welcome.php

<?php ...

public function index ()
{
    $view_data = array
    (
        'logged_in' => $this->Auth->logged_in()
    );
    $this->load->view('my_view', $view_data);
}

application/views/my_view.php

<? echo $logged_in ? 'Welcome back!' : 'Go login!' ?>
Joe
  • 15,669
  • 4
  • 48
  • 83
  • should `Auth` be lower case in `$this->Auth->logged_in()`? – dangel Jul 19 '13 at 21:36
  • 1
    @SvenB With most "older" frameworks (CI, Kohana 2.x, etc) the web root directory contains all of the application files (controllers, etc), so it's technically possible to just go to `/application/libraries/Auth.php` and have it load just that file. The `BASEPATH` constant is defined in the `index.php` file, so unless your request is going through that file (eg. a normal site request), that line stops you accessing the file. See also: http://en.wikipedia.org/wiki/Defence_in_depth – Joe May 12 '14 at 09:15
5

Are you using an authentication library? If not I'd suggest one. They come with functions like that.

Tank Auth for example has: is_logged_in() , which does exactly what you want!

http://www.konyukhov.com/soft/tank_auth/

For more info about which library to use you should check out this answer which compares all libs: https://stackoverflow.com/a/476902/576223

If you don't want an authentication library you can do as suggested by Joe

Community
  • 1
  • 1
bottleboot
  • 1,659
  • 2
  • 25
  • 41
  • I was just about to suggest **Tank Auth** as well, most people try to re-invent the wheel with frameworks, when there are already fantastic libraries around. – Jakub Feb 11 '12 at 01:59
  • What I suggested **is** an authentication library, just a very stripped down one to give him an idea of how it works and how to control the app flow :P – Joe Feb 11 '12 at 01:59
  • Yeh I know, i was just writing my answer as yours came up so I edited it. :) – bottleboot Feb 11 '12 at 02:01
  • What I saw is that Tank Auth is a little bit outdated since there is an awesome form validation function in Code Igniter: `is_unique[table.field]` but Tank Auth doesn't use it yet, but it's a really nice library, CI is my first framework I'm learning and I didnt know there are such great libraries like this one. Well, I'll use what Joe suggested, and then I'll copy some goodies from the Tank Auth library :). Thanks! – Cyclone Feb 11 '12 at 09:23
  • Your welcome! That's why we are here for, to help :). CI was my first framework too and I went through similar issues. Tank Auth is currently actively being updated to v2, so it might be worth opening a ticket asking to use this function https://github.com/ilkon/Tank-Auth/issues?state=open – bottleboot Feb 12 '13 at 15:14
0

you can use MY_controller with all function needed on every page of your website. and inherit all controllers from it. read this oficial wiki

jay
  • 44
  • 4