Login for Individual Pages
When you want a single web page to be password protected, insert a small block of PHP code on the page.
The code is intended for individual web pages meant to be accessible only by certain people. No password-protected directory required. No membership system required. And the system is quick to implement.
How it works:
-
A browser arrives at the URL of the web page. A log-in form is presented instead of the page's protected content.
-
The person types in the username and password, and taps the button.
-
The same page reloads. If the username and password were correct, the protected content is displayed in the browser.
No cookie is set. No personal information is stored anywhere else with this system. Thus, there should be no issues with any government privacy regulations.
The block of PHP code has two customizations where you specify the username and the password. Other than that, it is copy and paste. Paste it into the web page somewhere above the content to protect.
The block of PHP code is a short thing, less than 20 lines. It can be used in the source code of any *.php
web page.
Save it at a handy spot on your hard drive, or bookmark this page, so it is easy to copy and paste. If on your hard drive, an example page with the code, along with comments, may assist remembering how it's used.
In other words, let it be available for whenever you need to restrict access to a web page.
Below is the PHP code. Notes follow.
<?php // Single-Page Authentication, Version 1.0, August 1, 2021 // // Will Bontrager Software LLC, https://www.willmaster.com // $Username = "LogInUsername"; // case-insensitive $Password = "LogInPassword"; // case-sensitive if(empty($_POST['u']) or empty($_POST['p']) or strtolower($_POST['u'])!=strtolower($Username) or $_POST['p']!=$Password){ echo <<<LOGINFORM <form method="post" action="{$_SERVER['PHP_SELF']}" style="display:inline-block;"> <p>Username<br><input type="text" name="u" style="width:200px;"></p> <p>Password<br><input type="password" name="p" style="width:200px;"></p> <p><input type="submit" value="Log In" style="width:200px;"></p> </form> LOGINFORM; exit; } ?>
There are two customizations. Then the above code is ready to paste into the web page with content you want to protect.
-
Replace
LogInUsername
with the username to be provided for accessing the protected content. The username is case-insensitive. UserName is seen as the same as username. -
Replace
LogInPassword
with the password to be provided. This is case-sensitive. Any capital letters or lower-case letters you provide here must be typed exactly that way for the login to succeed.
Place the customized PHP code into the web page anywhere above the content to be protected.
It needs to be somewhere above the content to be protected so the login form will appear and stop the page display before the protected content is published.
Once you've done it, you'll see how easy it really is to block people from viewing the content you are protecting. (It also blocks search engine spiders, by the way.)
Only people with the username and password you specified for the page will be able to log in and view the protected content.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager