burger menu icon
WillMaster

WillMaster > Library > Security 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!

Access-Restricted Page

Let's say you have a content page for only one or a few people. Perhaps a download link. Or a secret message. Something.

You want to keep search engines out of it. And also AI slurpers.

Also a log file so you can see when it was accessed.

And, yes, it should be simple.

As long as we're at it, can it please work without cookies?

If you're smiling, and even if you're not, today's article provides the code for that.

It's one file. The top part lets you provide one or more access codes. Folks will need one of these to gain access to your content. Further below in the file is where you provide the content.

The code is below. Notes follow.

<?php
/*
Access-Restricted Page
Version 1.0
October 10, 2026
Will Bontrager Software LLC
https://www.willmaster.com/
*/
// Specify one access code per line between lines containing word CODELIST
$AccessCodes = <<<CODELIST
c.o.d.e
Every Tuesday
humanize
CODELIST;
$logfilename="loginlog.txt";
$passed=false;
if(isset($_POST['CODE']))
{
   $ta=array();
   foreach(preg_split('/[\r\n]/',trim($AccessCodes)) as $code)
   {
      if(!preg_match('/\S/',$code)){continue;}
      $ta[]=strtolower(trim($code));
   }
   $putcode = strtolower(trim($_POST['CODE']));
   foreach( $ta as $code )
   {
      if($putcode==$code)
      {
         file_put_contents(__DIR__."/$logfilename",date('r')."\t{$_POST['CODE']}\t{$_SERVER['REMOTE_ADDR']}\t{$_SERVER['HTTP_USER_AGENT']}r\n",FILE_APPEND);
         $passed = true;
         break;
      }
   }
}
if(!$passed)
{
   echo <<<FORM
<!DOCTYPE html><html><head> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Provide Access Code</title></head><body>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<div style="max-width:3in; margin:.25in auto;">
<h3>Access Code:</h3>
<input type="text" style="font-size:2rem; width:100%;" name="CODE">
<p><input type="submit" value="Submit Code"></p>
</div></form></body></html>
FORM;
   exit;
}
?>


<!--
The section above is the PHP to manage the page access.
The section below is the web page that the logged-in person will see.
-->


<!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>YOUR PAGE TITLE</title>
<style type="text/css">
html { font-family:sans-serif; font-size:100%; }
a { text-decoration:none; }
</style>
</head>
<body>

[PAGE CONTENT GOES HERE]

</body>
</html>

Notes:

As mentioned, there are two sections. About a dozen lines from the top, you'll see this.

c.o.d.e Every Tuesday humanize

Those are codes (one line per code) that can be used to access the page. Replace those with your own list of codes. Each code on a line by itself. Codes may have spaces within them, but space at the ends will be ignored.

A few lines from the bottom of the file, you'll see this.

[PAGE CONTENT GOES HERE]

That's a placeholder for your content. Replace it with the content you want to present to those who gain access to your page.

Upload the page and test it.

When it works as it should, provide the URL and an access code to those who should have access.

(This content first appeared in 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-2026 Will Bontrager Software LLC