r/PHPhelp • u/Legal_Revenue8126 • Dec 17 '25
Solved Die/Exit Usage Best Practices?
I have some cases in my code where I utilize the die/exit function to kill the program as a means to throw an error message to a user and prevent unauthorized access to content. People seem to say to just avoid these functions altogether and just throw an exception, but that doesn't make sense to me in this situation.
For example, the following code:
if(!isset($_SESSION['loggedin'])){
echo "Unauthorized Access<br><br>Please <a href='userlogin.php'>Log In</a>";
exit(1);
}Would this be considered good practice, or is there a more ideal way to handle this?
Should I just auto-redirect to the login page instead?
5
Upvotes
View all comments
Show parent comment
1
u/martinbean Dec 17 '25
The “appropriate place” would obviously be the application’s error handler, where other exceptions are caught.