One Page for Password and Content
When delivering content restricted to only one person, the page itself can require a password for access.
With this technique, no special password-protected area is required. The protection is built into the page itself, and only that one page has the restriction.
It is quick to implement.
No cookie is involved.
How it Works
When the page is first accessed, the user sees a form where they can type in their password.
If the correct password is provided, the page is reloaded with the content revealed. (Otherwise, the password form is presented again.)
The page can have whatever valid web page content you want it to have.
Here is the source code. Customization notes follow.
<?php $password = "myPassword"; // Replace this password //---------------------- // Version 1.1 of February 2, 2021, 16:45 EST $passed = false; $password = strtolower($password); if( isset($_POST['access']) ) { $_POST['access'] = strtolower($_POST['access']); } if( empty($_POST['access']) or $_POST['access'] != $password ) { echo <<<FORM <div style="display:table; margin:.5in auto; font-size:1.2rem; border:3px solid #ccc; border-radius:1em; padding:1em;"> <form method="post" enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}"> Access code: <input type="text" name="access" style="width:3in; border:1px solid #666; border-radius:.25em; padding:.25em;"> <br><input type="submit" value="Submit Code"> </form> </div> FORM; exit; } ?><!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Test 1</title> </head> <body> SECRET CONTENT </body> </html>
Customization —
There are two places to customize.
-
Replace
myPassword
at the second line from the top of the above source code with the password to be required for access to the secret content of the page. -
Replace
SECRET CONTENT
at the third line from the bottom of the above source code with the HTML content to be revealed when the correct password is provided.
Put the page on your website and test it. It should work exactly as designed: Provide the password to see the content.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager