Welcome Guest, Not a member yet? Register   Sign In
problem with basic user authentication
#1

[eluser]bhakti.thakkar[/eluser]
hi all,
i am trying to create a check on my applications entry point. if the user tries to access the application without login, then he should be redirect() to login page. I am already using sessions in my application and i was shocked to see that if the user doesnt logout and tries to access the index.php, he is directly able to do it. I tried it with hook, but then it will even check my login page and display error message.

I also have a function in my application/libraries/site_sentry.php and i also have done this is in my autoload.php

$autoload['libraries'] = array('database', 'site_sentry', 'session');


site_sentry.php
Code:
function is_logged_in()
    {
        if ($this->obj->session) {

            //If user has valid session, and such is logged in
            if ($this->obj->session->userdata('sname'))
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
            
        }
        else
        {
            return FALSE;
        }
    }

but on my applications mainpage, this doesnt get called.

How will i do it. This is my first project with CI
Thanks
#2

[eluser]missionsix[/eluser]
including the file is not enough. you need to invoke the class. although i hope you knew this already.


site_entry.php
Code:
class site_entry {

   var obj;

   function site_entry() {
     $this->obj = get_instance();
    
    if(!$this->is_logged_in()) {
      redirect('login');
    }
   }

   function is_logged_in()
    {
        if ($this->obj->session) {

            //If user has valid session, and such is logged in
            if ($this->obj->session->userdata('sname'))
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
            
        }
        else
        {
            return FALSE;
        }
    }
}


I can already see a problem with this though, because every single page is restricted. even the login page, which would go into a redirection loop.
#3

[eluser]bhakti.thakkar[/eluser]
of course the function is_logged_in() is a member of site_sentry class. but the problem is the index.php is not called thru any controller. in my other pages, as the entry point is controller i do as below. there is one page certificate view and it gets invoked from a controller as below

class Certificates extends Controller {

function __construct()
{
parent::Controller();

if (!$this->site_sentry->is_logged_in()) {redirect('login');}
$this->load->helper('ajax');

}
}
and automatically the user gets redirected to login.

has anybody done anything to get this done???

Thanks in advance
#4

[eluser]bhakti.thakkar[/eluser]
Hi all,
i got through this problem. the problem was the sessions weren't ending on the close of browser. i was using the default session library provided by CI which was not enough for my application. So i scanned through different ways for incorporating sessions in CI. there are many OB sessions , native sessions , PHP sessions and many more. you really have to choose between the best suited for your application. i finally opted for Native sessions.

now all is fine. hope it helps someone




Theme © iAndrew 2016 - Forum software by © MyBB