views:

80

answers:

2

Hi everyone,

I am currently working on adding pre-validation to my website. So that pages that require a log-in or other criteria, will only display if your session matches that criteria. I've got most of the system working, but I have one major obstacle. I cannot figure out how to stop CodeIgniter from running the rest of the controller (and thereby showing the page anyways), when the validation fails.

Here is my configuration:

  • All my pages are in the Content controller
  • My security system is a model called security.php
  • In the _head private function (which is called by every page), I load security.php and call it's main function: run()
  • $this->security->run() gets the specific validation criteria for the page, and checks them.

    • If the user passes, then run() does nothing, and the page execution continues

    • This is where I need help. If the user does not pass, then I need to display an error page, and stop the controller from calling any other views.

Does anyone know how to do this?

Thanks,

Lemiant

+1  A: 

You could do one of two things. A would be to redirect to another page with a differant uri. B would be to use an if/else statement to choose which view you show under the same uri.

One thing you need to do is have the security method you talked about return TRUE or FALSE if it is successful or not.

Examples:

A:

if(!$this->security->run())
{
    redirect('my/error/page');
}

B:

if($this->security->run())
{
    // Security Passes, proceed as normal
}
else
{
    // Security Fails, show error page
}

Hope this helps

Tom Schlick
Thanks. That seems to be the only option.
lemiant
Could load a View for an error page instead of a redirect() so your not doubling the # of server requests.
Mitchell McKenna
@Mitchell McKenna - Yes i know thats what i gave option B. i gave option A in case he wanted to url to be like /error/unauthorized or something like that
Tom Schlick
A: 

How stop execution of codeigniter: die(); or exit();

But, I don't think that's really what you want to do. What you want to do is to load a different view to show an error page if the validation fails.

Mitchell McKenna