Quick and Easy Way to Check for Authentication in PHP

Recently started reviewing how authentication works with any general PHP code I write. This came across as a quick and easy way to achieve this goal.

Let’s dissect this code for a minute so you can fully understand it’s power:

== First ==

This is a straight “if” statement, no “else” or “elseif” — so it is purely checking if the statement is true or not.

== Second ==

This is placed at the top of the page, preferably in the header, so it is always used throughout all your views

== Third ==

The native PHP function for “defined” is great, (reference:¬†http://php.net/manual/en/function.defined.php)

== Fourth ==

If the statement is “NOT” defined, (notice the exclamation point), it will render out an header of “403 forbidden” and then “exit”.

== Lastly ==

In closing, there were a few articles on the internet talking about how “defined” is slow and it is faster to use “isset”, so feel free to edit as necessary. Cheers!


Was this blog post helpful for you?

Support my blog and donate!

My blog is open to the public and will always be freely available. With your donation, we can help others learn, together.

Donate in Bitcoin: