Password Protecting Pages with PHP

January 29, 2011

password protect pages with php


I recently got an email asking how to create password protected pages .. in an easy fast way for a beginner to PHP. From the email:

I was wondering if there is some premade script and setup for database I can utilize to get this up faster and still proceed to educate myself.

As any experience PHP nerd knows, there are several approaches one could take. But since this guy is new to PHP and programming, I decided on something simple:

Hi Bob,

First, let me thank you for your kind words about the site … I love hearing from people.

To answer your question; if you simply need to password protect a directory/folder in your site, you can simple use CPANEL to create something called an .htaccess file. This will will protect ALL your content in that directory. The thing is though is that you will have to manually add users to this file (it is a text file) and you have to have a Linux based server… most are these days.

That said, with a little PHP file manipulating knowledge, you could use PHP to dynamically add users to the .htaccess file. Another option is to use something called PHP sessions to password protect pages:

… it is found under:

More Basic PHP Continued

* Making decisions with PHP: conditional statements (14:35)
* PHP Loops (12:10)
* PHP Functions – part 1 (8:31)
* PHP Functions – part 2 (8:57)
* PHP Functions – part 3 (8:12)
* Creating Customs PHP Functions Part 1 (6:47)
* Creating Customs PHP Functions Part 2 (8:45)
* Creating Customs PHP Functions Part 3 (6:09)
* PHP Sessions Part 1 (4:55)
* PHP Sessions Part 2 (8:24)
* PHP Sessions Part 3 (7:08)

You can see the first of the sessions videos here:

There are other methods like using a CMS like Drupal or a blog like WordPress … they have built in ability to protect pages.

I hope that helps,



Comments are closed.

To Top