Password Protecting Pages With GoDaddy, PHP and mySQL

I host several domains with GoDaddy, and while they offer cheap prices, they also offer cheap service and features. Last week I needed to password protect some content. First, I tried to protect folders with htaccess and htpasswd files, but I could not get it to work. Several forums suggested GoDaddy hosting is not compatible with htaccess, so I went with the PHP-mySQL route instead. (note: here is a tutorial from GoDaddy for automating the htaccess process, which looks less-professional than the PHP solution because of the htaccess’s popup dialog box: GoDaddy Instructions). Credit to Ethan Nordness for the PHP code.

Step 1: Create a new mySQL Database

GoDaddy hosting gives you access to 10 mySQL servers for free with your account. Log into GoDaddy, click on Hosting, then Manage Account. Select Databases and click on MySQL. Then in the nav bar, Create Database. Be sure to save your record your database name, password, etc, as you will be using them later. Once your database is set up, click on Manage via phpMyAdmin. First, create a new table in your database. Then, add 4 fields with the following properties (leave all other properties as they are):


Field ## Type ## Length/Values ## Collation
uid ## int ## 30
username ## varchar ## 30 ## utf8_general_ci
password ## varchar ## 50 ## utf8_general_ci
creation ## varchar ## 30 ## utf8_general_ci

Step 2: Create User(s)

Select the table you made and click on the “Insert” tab at the top of the page. It is important that the Uid field is given a unique value. Passwords are case sensitive, Usernames are not, and Creation is for your own use/reference.

Step 3: Implement Code

The 3 code files are stored at .txt files for ease of making them available on the web, but all 3 should be .php files in your implementation.

login.php When users go to a page that implements the password protection, they are redirected here. Keep in mind that All the pages should/can be customized with HTML to match your website’s theme and layout.

Go To login.txt

auth.php The filename of this PHP file can be changed. For example, in our implementation, we call this file index.php so our users do not see the file name in the URL. Simply copy this bit of code to the top of any page you want password-protected and save the file as .php. Through initial testing, the password-protected pages need to be in sub folders of the location of the login.php file (or they could be in the same folder).

Go To auth.txt

logout.php Direct users to this page to end their current session.

Go To logout.txt

Further, tracking logins requires only a small code tweak, discussed here

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *