Password Protect Any Webpage – The SIMPLE Way!
Have you ever found the need were you need some protection for a single page on your website? You know, maybe the download page were the username and password are sent out to the user via email after they have purchased. Or maybe the fact that your only going to allow certain people to see your ’special offer’ that you spent ages creating.
Well today, I have your answer! And NO, it’s not going to cost you a single penny unless of course you do decide to invest in some gear with 1000’s more functions. If you want to protect MORE than the one page (eg, a membership area) at a time…than this code isn’t for you. Although, please do keep reading as you may need it for future.
So lets get onto the basics. The visitors are required to enter a username and password into the login form to view the page content. YOU decide these 2 values from inside the code, and to be honest…is perfect for protecting your download pages. You would just need to find a way of telling them what to use.
<?php
// Define your username and password
$username = "someuser";
$password = "somepassword";if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {
?><h1>Login</h1>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><label for="txtUsername">Username:</label>
<br /><input type="text" title="Enter your Username" name="txtUsername" /></p><p><label for="txtpassword">Password:</label>
<br /><input type="password" title="Enter your password" name="txtPassword" /></p><p><input type="submit" name="Submit" value="Login" /></p>
</form>
<?php
}
else {?>
<p>This is the protected page. Your private content goes here.</p>
<?php
}
?>
Now from past experience, I know that it’s NOT good to copy any code in Wordpress because it does tend to mess a few things up! So that’s why I’ve decided to help you out here. I have placed the code into a text file were you can visit and just copy then paste into what relevant file you like.
== >> http://www.mark-mcwilliams.com/passprotect.txt
When you do insert this code into your web-page and go to save, you’ll need to call it ’something.php‘ instead of the usual .html – I’m also hoping to implement this code into a few pages on my site. I’ve even setup a test page, and tried it out myself and it’s simple enough to do.
However…if you would rather buy some software, then there are really only 2 that I can recommend.
So anyway, please do let me know what you think in the comments below.
Hi Mark
Very easy approach to creating a general mass password protected page. Keeps the wanders and stumblers at bay, for a while anyway. The ultimate protection like you say is to spring for DL Guard, MyDD, or even EW-Portal, but for the beginning marketer limited on funds your approach will work just fine.
slater,
Rick (RedHat39)
http://www.daelectronics.com
Mark,
I really am no expert when it comes to code, but I still have a little tip to add……….you mention about wordpress going mad when you try to put code in there. True. Today I tried to drop in the embedding code for a youtube vid. Results in my template layout going crazy. Then I learned go to Wordpres Dashboard > Users > profile then un-check the “Rich Visual Editing” check-box. Now you can drop code (eg youtube embedded) into a new post click publish. Now go back to turn the Visual text editor back on.
I don’t know if this method could be applied to your password protect code.
Hey Mark,
Nice little article that! Another tip for you and your readers (if you are using Cpanel on your hosting) is to use the protection feature on there. It saves messing around with code. If you can’t find it let me know and I will show you
Thanks
Danny
http://www.DannyClare.com
Danny will you show me where cuz I’m lost, I use cPanel in Monster.
Thanks
Donnie