2

I'm building a site that has one authentication system for both general users and administrative users (using CodeIgniter and Tank Auth, for anyone interested). They are differentiated based on profile credentials. (Is this a secure approach?)

My question is, when a user accesses an area they do not have the credentials to view, what PHP functionality should I use to restrict access? My thoughts are either a 'redirect' or 'exit' function. What is the most secure approach?

Eric Di Bari
  • 3,767
  • 7
  • 40
  • 49

2 Answers2

5

Use both:

header("Location: http://your_login_page");
exit();

exit() will ensure that the remainder of the script does not get processed, and header() will send the user to a useful location. ("Idle hands are the devil's workshop...")

George Cummins
  • 28,485
  • 8
  • 71
  • 90
1

Well, I think that Tank Auth should be handling it already, isn't it? (I never used that library, Ion Auth does it and I assumed is a must have for an Authentication system). Usually, in plain Php, you might want to do a

header("Location: your/url");
exit();

when redirecting after checking for the logged status (via $_SESSION, $_SESSION + cookies, for. ex.). Exit() prevents the script to go on in case the header might fail, thus displaying unwanted code.

Codeingiter has its built-in redirect system (check here on User Manual )the redirect() function. You can use it like:

if ($logged_in == FALSE)
{
     redirect('/login/form/', 'refresh');
}

or using 'location' instead of 'refresh', witch allows you to set a header response to send to the server

// with 301 redirect
redirect('/article/13', 'location', 301);

But I believe Tank Auth has its own methods that are enough for your application, have you checked that already?

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • Tank Auth doesn't seem to handle user roles. I could add something to user profile data, but I still need to manually test the user's credentials and block/allow access appropriately. – Eric Di Bari Jun 13 '11 at 17:21
  • @Eric Di Bari check Ion Auth maybe, it has that features. Whatever you choose, you can use CI's native redirect function to help you in these. Remember to check for the credentials in controllers' constructors if you want to extend ACL to all the 'page' and its methods – Damien Pirsy Jun 13 '11 at 17:23