A-A+

一个用来操作apache的.htaccess文件的类

2012年05月03日 编程技术 暂无评论

htaccess的文件是Apache服务器下面的一个配置文件,主要负责相关目录下的网页配置,比如我们可以通过htaccess的文件实现,网页301重定向、自定义错误页、改变文件扩展名、php伪静态,目录权限的访问,配置默认文档等。

下面是一个用来操作Apache的.htaccess文件的类,大家可以了解一下即可,其实在Apache服务器下面会自动生成一个htaccess配置文件,如果有需要,可以直接进行更改。

htaccess.class.php 这个是类文件。

[code lang="php"]

* @copyright Intertribe - Internetservices Germany
* @include Funktion:_include_
*/

class htaccess{
var $fHtaccess=""; // path and filename for htaccess file
var $fHtgroup=""; // path and filename for htgroup file
var $fPasswd=""; // path and filename for passwd file

var $authType="Basic"; // Default authentification type
var $authName="Internal area"; // Default authentificat
ion name

/**
* Initialising class htaccess
*/
function htaccess(){
}

/**
* Sets the filename and path of .htaccess to work with
* @param string $filename the name of htaccess file
*/
function setFHtaccess($filename){
$this->fHtaccess=$filename;
}

/**
* Sets the filename and path of the htgroup file for the
htaccess file
* @param string $filename the name of htgroup file
*/
function setFHtgroup($filename){
$this->fHtgroup=$filename;
}

/**
* Sets the filename and path of the password file for the
htaccess file
* @param string $filename the name of htgroup file
*/
function setFPasswd($filename){
$this->fPasswd=$filename;
}

/**
* Adds a user to the password file
* @param string $username Username
* @param string $password Password for Username
* @param string $group Groupname for User (optional)
* @return boolean $created Returns true if
user have been created otherwise false
*/
function addUser($username,$password,$group){
// checking if user already exists
$file=@fopen($this->fPasswd,"r");
$isAlready=false;
while($line=@fgets($file,200)){
$lineArr=explode(":",$line);
if($username==$lineArr[0]){
$isAlready=true;
}
}

if($isAlready==false){
$file=fopen($this->fPasswd,"a");

$password=crypt($password);
$newLine=$username.":".$password."\n";

fputs($file,$newLine);
fclose($file);
return true;
}else{
return false;
}
}

/**
* Adds a group to the htgroup file
* @param string $groupname Groupname
*/
function addGroup($groupname){
$file=fopen($this->fHtgroup,"a");
fclose($file);
}

/**
* Deletes a user in the password file
* @param string $username Username to delete
* @return boolean $deleted Returns true if user
have been deleted otherwise false
*/
function delUser($username){
// Reading names from file
$file=fopen($path.$this->fPasswd,"r");
$i=0;
while($line=fgets($file,200)){
$lineArr=explode(":",$line);
if($username!=$lineArr[0]){
$newUserlist[$i][0]=$lineArr[0];
$newUserlist[$i][1]=$lineArr[1];
$i++;
}else{
$deleted=true;
}
}
fclose($file);

// Writing names back to file (without the user
to delete)
$file=fopen($path.$this->fPasswd,"w");
for($i=0;$ifPasswd,"r");
$x=0;
for($i=0;$line=fgets($file,200);$i++){
$lineArr=explode(":",$line);
if($username!=$lineArr[0] && $lineArr[0]!=""
&& $lineArr[1]!=""){
$newUserlist[$i][0]=$lineArr[0];
$newUserlist[$i][1]=$lineArr[1];
$x++;
}else if($lineArr[0]!="" && $lineArr[1]!=""){
$newUserlist[$i][0]=$lineArr[0];
$newUserlist[$i][1]=crypt($new_password)."\n";
$isSet=true;
$x++;
}
}
fclose($file);

unlink($this->fPasswd);

/// Writing names back to file (with new password)
$file=fopen($this->fPasswd,"w");
for($i=0;$iauthType=$authtype;
}

/**
* Sets the Authentification Name (Name of the login area)
* @param string $authname Name of the login area
*/
function setAuthName($authname){
$this->authName=$authname;
}

/**
* Writes the htaccess file to the given Directory and
protects it
* @see setFhtaccess()
*/
function addLogin(){
$file=fopen($this->fHtaccess,"w+");
fputs($file,"Order allow,deny\n");
fputs($file,"Allow from all\n");
fputs($file,"AuthType ".$this->authType."\n");
fputs($file,"AuthUserFile ".$this->fPasswd."\n\n");
fputs($file,"AuthName \"".$this->authName."\"\n");
fputs($file,"require valid-user\n");
fclose($file);
}

/**
* Deletes the protection of the given directory
* @see setFhtaccess()
*/
function delLogin(){
unlink($this->fHtaccess);
}
}
?>
[/code]

examples.php 这个是调用的实例。

[code lang="php"]
setFPasswd("/var/www/htpasswd");

// Setting up path of password file
$ht->setFHtaccess("/var/www/.htaccess");

// Adding user
$ht->addUser("username","0815");

// Changing password for User
$ht->setPasswd("username","newPassword");

// Deleting user
$ht->delUser("username");

// Setting authenification type
// If you don't set, the default type will be "Basic"
$ht->setAuthType("Basic");

// Setting authenification area name
// If you don't set, the default name will be "Internal Area"
$ht->setAuthName("My private Area");

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// finally you have to process addLogin()
// to write out the .htaccess file
$ht->addLogin();

// To delete a Login use the delLogin function
$ht->delLogin();
?>
[/code]

标签:

给我留言