I am trying to keep my code as secure/clean as possible. If I have an If/Else statement that checks for whether or not the user is logged in (if not, the user is redirected back to the home page), will either of these methods work?
if (!$this->auth->is_logged_in()) {
redirect('');
} else {
// do something top secret
}
versus
if (!$this->auth->is_logged_in()) redirect('');
// do some top secret stuff
I would prefer the second method, but I am unsure if it is secure.
Edit
These would be used in a codeigniter controller
Another Edit
In codeigniter the redirect()
function includes the exit;
discussed in the answers below.
Either is fine, but braces are generally preferred in case you want to add functionality later. I would also create a method in auth such as
gatekeeper()
,force_login()
, whatever that does this check but redirects internally as you will probably use this a lot and it will be annoying to write this if statement check everywhere.There's no issue with security here.
I would remove the else if possible:
I think it's the most readable.
Also, I'm sure it does, but make sure the redirect function includes a call to
exit