Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibrarySecurity and Blocking

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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:

  1. A browser arrives at the URL of the web page. A log-in form is presented instead of the page's protected content.

  2. The person types in the username and password, and taps the button.

  3. 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.

  1. 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.

  2. 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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC