Welcome Guest, Not a member yet? Register   Sign In
Make sure user is logged in then show pages EXCEPT for login page.
#1

[eluser]behnampmdg3[/eluser]
Hi;

I have to make sure visitors cannot see any page but login page. So if they are in any other page they get redirected to login page.

I can add a "session check and redirect to login" in _constructor of all controlers but I prefer add it in 1 place and so I wont have to change everything.

Please advice.

Thanks
#2

[eluser]pzntec[/eluser]
Sure that sounds fine. Create a session controller and redirect the login controller if no session exists.

Be sure to take a look at the session library that comes with CI. http://ellislab.com/codeigniter/user-gui...sions.html
It may come in handy.
#3

[eluser]behnampmdg3[/eluser]
[quote author="sandstorm140" date="1370168422"]Sure that sounds fine. Create a session controller and redirect the login controller if no session exists.

Be sure to take a look at the session library that comes with CI. [/quote]Please show example of this.
Thanks
#4

[eluser]Pert[/eluser]
[quote author="behnampmdg3" date="1370160864"]
I can add a "session check and redirect to login" in _constructor of all controlers but I prefer add it in 1 place and so I wont have to change everything.
[/quote]

You can create a user model and autoload it, then check login state in model __construct method. When not logged in and not on login page, redirect to login page.
#5

[eluser]behnampmdg3[/eluser]
[quote author="Pert" date="1370173285"]You can create a user model and autoload it, then check login state in model __construct method. [/quote]Hi;

I want some code example and also need to know where should I save it? Also I think it should be controller not model.

Thanks
#6

[eluser]Pert[/eluser]
[quote author="behnampmdg3" date="1370180842"]Also I think it should be controller not model.[/quote]

If you think about it, user is a model, right?

So why not make this user model to automatically get user session before anything else happens and redirect if user is trying to access pages it should not access?

If you add this code to every single controller, you're just duplicating code.

When you forgot to add that check to one of the controllers, random visitors will have access to part of your website, so why not do it automatically, doesn't matter if you or anyone else has to add controllers in the future and forgets or did not know to add check routines.

If users have different access levels, then sure, there will be some checks in individual controllers, for example random person should not edit someone elses post like it was their own, but to cover basic log in, model is the way to go.




Theme © iAndrew 2016 - Forum software by © MyBB