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?

3 Answers 3

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!' ?>
Sign up to request clarification or add additional context in comments.

3 Comments

should Auth be lower case in $this->Auth->logged_in()?
what is the <?php defined('BASEPATH') OR exit('No direct script access allowed'); line for ?
@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: en.wikipedia.org/wiki/Defence_in_depth
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

5 Comments

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.
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
Yeh I know, i was just writing my answer as yours came up so I edited it. :)
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!
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 github.com/ilkon/Tank-Auth/issues?state=open
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.