As a final part of a three part series in how to password protect your pages with your new user database that you created from the previous posts. To do this, create a new php file called:
passprotect.php
<?php
include ‘config.php’;
$authorid = $_COOKIE["member"];
$password = $_COOKIE["memberpass"];
$SQLvalidate = “SELECT * FROM `member` WHERE memberid=’$memberid’ AND password=’$password’”;
$results = mysql_query($SQLvalidate);
$numrows = mysql_num_rows($results);
if ($numrows == 0) {
include ‘login.php’;
die();
}?>
You may notice a cookie is requested that has not been previously mentioned in other posts. To password protect your pages, it is better to reference a password and memberid comination to prevent unauthorized access. To declare this cookie simply add this line next to the authorid cookie declaration.
setcookie(“memberpass”,”$password”, time()+3600 *2, “/”, “.blazekwebdesign.com”);
Now if you want to password protect a page simply add this line to the top of any page, but make sure you do not have the “include ‘config.php’” before or after because that file is already included in my previous examples and will cause an error. Otherwise, just add this line to THE TOP of any file you want to password protect:
include ‘passprotect.php’;
This is very straight forward, but effective and it is meant to be used in conjunction with the previous posts.
Part 1: How to create a PHP membership database







