I've been building a web application and I ran into a rather annoying problem.
My current layout is this:
I have a single file that performs all of the function calls that will inevitably generate the page. Before this file actually generates a viewable page, it first checks if the user is logged in. If the user is logged in, we display the page. If the user is not logged in, we redirect to a login page. Similarly, if the user is logged in and tries to view the login page, the user will be redirected to their home page.
My problem is rather philosophical, but I feel that the redirect is unnecessary as I can easily print the page that the user should be redirected to without performing the redirection. The problem with this method is this does not edit the url.
Here is an example:
- A user navigates to the page /app/home, which is restricted to logged in users. We hit the database to see if the user is logged in. They are not, so we redirect them to /app/login, which is restricted to users who are not logged in. We hit the database to see if the user is not logged in. They are not, so we display the /app/login page.
The redirection step seems unnecessary, since it adds another unnecessary database hit. I would like it go this way:
- A user navigates to the page /app/home, which is restricted to logged in users. We hit the database to see if the user is logged in. They are not, so we change the displayed url to /app/login and display the /app/login page.
I want to point out that I don't have a performance issue with this. The second method just seems more elegant. I am aware that there is a javascript method that allows you to modify the url, but I would prefer to do something like this is php, as I haven't left the server yet.