Code
<?php
/************************************************************************/
/* PHP Simple PasswordProtect v1.0 */
/* =========================== */
/* */
/* Written by Steve Dawson - http://www.stevedawson.com */
/* Freelance Web Developer - PHP, MySQL, HTML programming */
/* */
/* This program is free software. You can redistribute it and/or modify */
/* but please leave this header intact, thanks */
/************************************************************************/
##########################################################################
/**
* Protector: A simple class adding password protection
* to a code block, based on the above guy's code
*
* Outline:
* $p=Protector(<password>)
* $p->protect($html);
*
* Author: Jonathan Scholz
* 2/10/2012
*/
class Protector
{
/**
* Property declarations:
*/
protected $password = "password";
function __construct($password) {
$this->password = $password;
}
public function protect($html) {
// If password is valid let the user get access
if ((isset($_POST["password"]) && ($_POST["password"]==$this->password))) {
print "<!-- ================== START OF HIDDEN HTML - PLACE YOUR CONTENT HERE ====== -->";
print $html;
print "<!-- ================== END OF HIDDEN HTML ================================== -->";
}
else
{
// Wrong password or no password entered display this message
if (isset($_POST['password']) || $this->password == "") {
print "<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b><br>Please enter the correct password</font></p>";}
print "<form method=\"post\"><p align=\"center\">Password required<br>";
print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Submit\" type=\"submit\"></p></form>";
}
}
/**
* Authenticate a user against a password file generated by Apache's httpasswd
* using PHP rather than Apache itself.
* from: http://koivi.com/php-http-auth/
*
* @param string $user The submitted user name
* @param string $pass The submitted password
* @param string $pass_file='.htpasswd' The system path to the htpasswd file
* @param string $crypt_type='DES' The crypt type used to create the htpasswd file
* @return bool
*/
function http_authenticate($user,$pass,$pass_file='.htpasswd',$crypt_type='DES'){
// the stuff below is just an example useage that restricts
// user names and passwords to only alpha-numeric characters.
if(!ctype_alnum($user)){
// invalid user name
return FALSE;
}
if(!ctype_alnum($pass)){
// invalid password
return FALSE;
}
// get the information from the htpasswd file
if(file_exists($pass_file) && is_readable($pass_file)){
// the password file exists, open it
if($fp=fopen($pass_file,'r')){
while($line=fgets($fp)){
// for each line in the file remove line endings
$line=preg_replace('`[\r\n]$`','',$line);
list($fuser,$fpass)=explode(':',$line);
if($fuser==$user){
// the submitted user name matches this line
// in the file
switch($crypt_type){
case 'DES':
// the salt is the first 2
// characters for DES encryption
$salt=substr($fpass,0,2);
// use the salt to encode the
// submitted password
$test_pw=crypt($pass,$salt);
break;
case 'PLAIN':
$test_pw=$pass;
break;
case 'SHA':
case 'MD5':
default:
// unsupported crypt type
fclose($fp);
return FALSE;
}
if($test_pw == $fpass){
// authentication success.
fclose($fp);
return TRUE;
}else{
return FALSE;
}
}
}
fclose($fp);
}else{
// could not open the password file
return FALSE;
}
}else{
return FALSE;
}
}
}About me