Skip to content

Instantly share code, notes, and snippets.

@tobinski
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobinski/78dbc1a245fbd82cfe2f to your computer and use it in GitHub Desktop.
Save tobinski/78dbc1a245fbd82cfe2f to your computer and use it in GitHub Desktop.
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