Skip to content

Instantly share code, notes, and snippets.

@blakethepatton
Last active August 7, 2018 22:24
Show Gist options
  • Save blakethepatton/d5b7bde5b303fee03392f30c9927dba3 to your computer and use it in GitHub Desktop.
Save blakethepatton/d5b7bde5b303fee03392f30c9927dba3 to your computer and use it in GitHub Desktop.

DNS Based website

I had a crazy idea, and wanted to be able to store a website within the DNS system.

I mean, I guess it's not that crazy, but I couldn't find anyone else doing it on the

internet. Ideally, it'd be rendered by javascript, but it seems that JS doesn't have

that native ability. And to get txt dns records inside of javascript you have to hit

an api. So screw the api, lets just do it in php.

I ended up doing it, it's a super simple implementation, you could very easily extend

it to have some sort of markdown generator render the content better. Either way, I

thought I'd share this. Below is an example list of txt records.

hello.domainname.tld TXT records

10: hello there! <br>
20: this is silly no?
hehehehe

the script at dns.php?q=hello would output

Hello there! <br>this is silly no?hehehehe

<?php
$q = (isset($_REQUEST['q'])) ? $_REQUEST['q'] : '';
$records = dns_get_record("{$q}.domainname.tld", DNS_TXT);
$return = [];
if($records===false)exit( "Nope. ") ;
foreach($records as $record){
$arr = explode(':', $record['txt'], 2);
if(preg_match("/:/", $record['txt'])==true && is_numeric($arr[0])){
$return[$arr[0]] = $arr[1];
} else {
$return[] = $record['txt'];
}
}
ksort($return);
echo implode($return, "");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment