Best way to Redirect if user not logged in ?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Fran3
New php-forum User
New php-forum User
Posts: 14
Joined: Sun Oct 08, 2017 4:01 pm

Tue Mar 06, 2018 4:32 pm

Is this code at the beginning of each page for logged in users... the right way to manage this?

Code: Select all

<?php	// Rediredt the user to the login page if not logged in

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == false) { 
     header('Location: http://www.login.php/'); 
} 

// normal page code goes here
?>
Thanks for any help.[/size]
User avatar
phpRob
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Tue Mar 06, 2018 5:48 pm

Code: Select all

function redirectIfNotLoggedIn(){
	//By having this in a function, you can maintain it in just one central place.
	if(isset($_SESSION['loggedin'])===false || $_SESSION['loggedin'] !== true) { 
	     header('Location: folderName/loginPageName.php'); 
	     exit;
	} 
}

Code: Select all

redirectIfNotLoggedIn(); //have this function call at the top of your pages.
Not many know this, but you should always call "exit;" to make sure no code gets executed after the redirect (it happens if you don't call "exit;". Check the improved if-statement test-condition. What you had actually would not do what you want.
Fran3
New php-forum User
New php-forum User
Posts: 14
Joined: Sun Oct 08, 2017 4:01 pm

Wed Mar 07, 2018 6:04 am

Thank you phpRob.
Question:
Where would you put the function so that it was accessible by every page?
Or would you put it in an 'include' file

include files are cool for keeping the main page readable

alternately putting this three lines of code at the top of each page

Code: Select all

	if(isset($_SESSION['loggedin'])===false || $_SESSION['loggedin'] !== true) { 
	     header('Location: folderName/loginPageName.php'); 
	     exit;
	} 
won't clutter the code too much... OR am I unaware of something like a PHP Global Function ?

Thanks again for the help and for fixing the 'if' statement for me :-)
chorn
php-forum GURU
php-forum GURU
Posts: 637
Joined: Fri Apr 01, 2016 2:18 am

Wed Mar 07, 2018 6:07 am

Where would you put the function so that it was accessible by every page?
In the router.
User avatar
phpRob
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Wed Mar 07, 2018 7:29 am

chorn is right. In the "router"--that is best. But what is that and do you yet have one? Likely not yet, but do work towards creating one at this time in your development efforts. Why are routers best? Because "routers" are ideal for centrally controlling what goes on in a set of site/application pages. It controls and regulates the functionality that your site/app offers to the outside world. Some call the "router" the "controller". I use the term "controller" in my head. But what really matters is that you start to think about how to make your job easier in terms of development and also security. "Centrally located and managed code." "Reusable code" You want these things. And I can tell you do. You did good by starting this post in the first place because it says you wanted to move in that direction.
Fran3
New php-forum User
New php-forum User
Posts: 14
Joined: Sun Oct 08, 2017 4:01 pm

Wed Mar 07, 2018 11:58 am

So the router or controller... this is like part of an MVC framework ... right?
User avatar
phpRob
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Wed Mar 07, 2018 12:57 pm

Yes, router/controller is a component of that "framework". MVC is not so much of a framework though it is often called that to add to its sexy appeal. It is a buzz-acronym that is not much more than a way of expressing the application of good programming practices. Sure, read some articles on it, see people writing about it and sometime garbling it up, and then move on and do what you know to be good and best from the takeaways you get. I talk like this because I believe it has managed to cause more confusion than anything.
Fran3
New php-forum User
New php-forum User
Posts: 14
Joined: Sun Oct 08, 2017 4:01 pm

Wed Mar 07, 2018 2:47 pm

Yes, I don't use frameworks as I try more or less to be a base coder/programmer and just write and organize the code myself.

It seems to me the more you rely on a system to help you produce the software the less you actually write in the language you are using and as time goes on you depend more and more on the system or framework to help you. Ditto for things like jquery.

Of course you can get done faster but if speed in your goal just buy an off the shelf program. (Only kidding, of course :-)

But, the next level up for fast output is Wix or some other such application where you hardly need to know programming at all.

But enough of this as may a good web developer produces perfectly usable output with these tools and it seems to now be the normal. No worries... me and Notepadd++ will just keep our trudging along until I see the light :-)
Post Reply