Skip to content

Instantly share code, notes, and snippets.

@tobinski
Last active Aug 29, 2015
Embed
What would you like to do?
Small script for the dynamic creation of htaccess
# .htaccess
Allow from hostname.dyndns.com
# This is not working
<?php
/**
* Config
**/
$ouputFile = "/httpdocs/.htaccess";
$config = "./htaccess.txt";
$output = "order deny,allow\n";
$output .= "deny from all\n";
$handle = fopen($config, "r");
$outputHandler = fopen($ouputFile, "w");
// read config
if ($handle) {
while (!feof($handle))
{
$line = fgets($handle);
if(preg_match($line, "/#"/)) continue;
if(is_valid_ip_address($line))
{
$ip = $line;
}
else
{
$ip = gethostbyname(trim($line));
}
$output .= "# ".trim($line)."\n";
$output .= "Allow from ".$ip."\n";
$ip=0;
}
fclose($handle);
}
// write htaccess
if($outputHandler)
{
fwrite($outputHandler, $output);
fclose($outputHandler);
}
function is_valid_ip_address ($string)
{
return preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $string, $result);
}
?>
# all the IP/hostnames and DynDNS-Hostnames you like to allow
hostname.dyndns.com
200.122.23.66
hostname2.dyndns.org
captns.ch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment