Solved

php script to protect a folder with .htaccess and .htpasswd

Posted on 2010-11-24
5
548 Views
Last Modified: 2012-05-10
Hi
I am looking for a simple php script that will generate (create .htaccess and .htpassword) to protect a folder

as simple as that !
ex: **protect.php?username=admin&password=admin**
and it will create automatially the .htaccess and .htpasswd that protects the folder with user admin/amin

Regards
0
Comment
Question by:yarekGmail
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:ropenner
Comment Utility
<?PHP
//$username = "admin";
//$password = "admin";
$username = $_Request("username");
$password = $_Request("password");

$path = pathinfo(__FILE__,PATHINFO_DIRNAME) . "/";

$default_htaccess_text = <<< endofhtaccesstext
AuthUserFile    $path.htpasswd
AuthGroupFile   /dev/null
AuthName        "Please Enter your Password for this Folder"
AuthType        Basic
<Limit GET POST>
require valid-user
</Limit>
endofhtaccesstext;

$username = preg_replace("/\W/","",$username); // remove any special characters only allowing letters and numbers and underscore .... customize this so system command doesn't become a security hole
$password = preg_replace("/\W/","",$password);

$create = (file_exists("$path.htaccess")?"":"-c");
system("htpasswd $create -b .htpasswd $username $password");
if (!file_exists("$path.htaccess")) {
      if (writeFile("$path.htaccess", $default_htaccess_text)) {
            print "successful write of .htaccess";
      } else {
            print "problem writing .htaccess file";
      }
}

function writeFile($file, $message) {
      if ($handle = fopen($file, "w")) {
            fwrite($handle, imap_qprint($message));
            fclose($handle);
            return true;
      } else {
            return false;
      }
}
?>
0
 
LVL 6

Accepted Solution

by:
V4nP3rs13 earned 500 total points
Comment Utility
No.. that code that ropenner posted is wrong. I think It has some mistakes.... here's the right code:
<?PHP
//$username = "admin";
//$password = "admin";
$username = $_REQUEST["username"];
$password = $_REQUEST["password"];

$path = pathinfo(__FILE__,PATHINFO_DIRNAME) . "/";

$default_htaccess_text = <<< endofhtaccesstext
AuthUserFile    $path.htpasswd
AuthGroupFile   /dev/null
AuthName        "Please Enter your Password for this Folder"
AuthType        Basic
<Limit GET POST>
require valid-user
</Limit>
endofhtaccesstext;

$username = preg_replace("/\W/","",$username); // remove any special characters only allowing letters and numbers and underscore .... customize this so system command doesn't become a security hole
$password = preg_replace("/\W/","",$password);

$create = (file_exists("$path.htaccess")?"":"-c");
system("htpasswd $create -b .htpasswd $username $password");
if (!file_exists("$path.htaccess")) {
      if (writeFile("$path.htaccess", $default_htaccess_text)) {
            print "successful write of .htaccess";
      } else {
            print "problem writing .htaccess file";
      }
}

function writeFile($file, $message) {
      if ($handle = fopen($file, "w")) {
            fwrite($handle, imap_qprint($message));
            fclose($handle);
            return true;
      } else {
            return false;
      }
}
?>

Open in new window

0
 

Author Comment

by:yarekGmail
Comment Utility
I executed the script and got : INTERNAL ERROR
It seems the .htpasswd was not written

It seems I cannot execute that on my shared hosting plan:
system("htpasswd $create -b .htpasswd $username $password");

Any idea on how to simply do that ?

regards
0
 
LVL 8

Expert Comment

by:ropenner
Comment Utility
I assumed UNIX or LINUX, what type of box are you hosted on (windows, unix)?

try just the part of creating the .htaccess file

if (writeFile("$path.htaccess", $default_htaccess_text)) {
   print "successful write of .htaccess";
} else {
   print "problem writing .htaccess file";
}

if you can do that ... then at least you have permission to create files and there may be a way to create the .htpasswd file without using a 'system' call.

On your shared hosting ... what method is provided to manually create the .htpasswd files?
0
 

Author Comment

by:yarekGmail
Comment Utility
Linux shared server.
There is no problem to create files with writeFile.
the .htaccess was created

But .htpasswd is not created :
it means system("htpasswd $create -b .htpasswd $username $password"); is NOT executed correctly

Regards
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now