r/PHPhelp • • 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

24 comments sorted by

View all comments

Show parent comment

1

u/colshrapnel Dec 17 '25

Don't you understand that your "obviously" could be "totally unknown" for someone else?

2

u/martinbean Dec 17 '25

Which is why I’ve updated my comment to add clarification and additional details.