How this website was made. PHP, CSS, and Folders.

Disclaimer: This is nothing fancy. I started out with HTML back in the 90's, started using Wordpress in the 00's, and lost interest in the 10's. I am a casual computer user, and didn't keep up with the times. I wanted something I could keep in one folder on my Desktop again. There is a lot of great flat file code out there, but I wanted something I could understand and control. I have tried to keep any code that I borrowed with the authors name. First things first.

Here is what the public_html folder looks like

The main .htaccess file

The .htaccess file redirects pages so they look like they are all in folders, when it is actually a file. Any requests for http://dspl.ca/j, or http://dspl.ca/j/ will be shown the page j.php and the same for the other user pages. It also strips any www. from requests.


RewriteCond %{HTTPS} =on
RewriteCond %{HTTPS} ^on$
	RewriteRule ^(.*)$ http://dspl.ca/$1 [R=301,L]
   	RewriteRule ^j$ j.php [L]
   	RewriteRule ^j/$ j.php [L]
	RewriteRule ^cori$ cori.php [L]
	RewriteRule ^cori/$ cori.php [L]
   	RewriteRule ^anna$ anna.php [L]
   	RewriteRule ^anna/$ anna.php [L]
RewriteCond %{HTTP_HOST} ^www\.
	RewriteCond %{HTTP_HOST} ^www.dspl.ca$ [NC]
	RewriteRule ^(.*)$ http://dspl.ca/$1 [R=301,L]

The main index.php

Basic includes to header.php and footer.php

<?php include("./header.php"); ?>
<h1>Dspl.ca Homepage</h1>
<p>Welcome to our little spot on the Internet. Finally got it back to the basics. This site is made only from <a href="http://dspl.ca/categories/Code/How%20this%20website%20was%20made.php">a few PHP files and a flat file directory structure</a>.</p>
<p>Things will get broken, and things will get better. Cheers.</p>

<p><i>Proudly powered by a <a href="http://tarot.freeshell.org/leafpad/">Text Editor</a> and some <a href="https://duckduckgo.com/">Internet Searches</a>.</i></p>
<?php include("./footer.php"); ?>
                    

The header.php

It is mostly the original header.php out of my wordpress installation so I could keep my existing style.css file. After that is a piece of code I added and modified by Chirp Internet: www.chirp.com.au that takes the contents of my categories folder (in the public_html root), displays each folder name, and links to each folders index.php.

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Dspl.ca</title>
<link rel='stylesheet' href='../css/style.css' type='text/css' media='all' />
<style type="text/css" id="custom-background-css">
body.custom-background { background-image: url('../bg/bg.jpg'); background-repeat: no-repeat; background-position: top left; background-attachment: fixed; background-size:100%; }
</style>
</head>

<body class="page page-template-default custom-background full-width">
<div id="titlediv"><h1 id="title" title="Hocus Pocus Black Magic"><a href="http://dspl.ca/">Dspl.ca</a></h1></div>
<div id="page" class="hfeed site">
<header id="masthead" class="site-header" role="banner">
        
<!-- #site-navigation -->
<nav id="site-navigation" class="main-navigation" role="navigation">
            <div class="menu-menu1-container"><ul id="menu-menu1" class="nav-menu"><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/cori/">Cori&#8217;s Stuff</a></li><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/j/">J&#8217;s stuff</a></li><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/anna/">Anna&#8217;s Stuff</a></li>

<?PHP
  
// Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.
  
function getFileList($dir)
  {
    
// array to hold return value
    
$retval = array();
    
// add trailing slash if missing
    
if(substr($dir, -1) != "/"$dir .= "/";
    
// open pointer to directory and read list of files
    
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(
false !== ($entry $d->read())) {
      
// skip hidden files
      
if($entry[0] == ".") continue;
      if(
is_dir("$dir$entry")) {
        
$retval[] = array(
          
"name" => "$dir$entry/",
          
"type" => filetype("$dir$entry"),
          
"size" => 0,
          
"lastmod" => filemtime("$dir$entry")
        );
      } elseif(
is_readable("$dir$entry")) {
        
$retval[] = array(
          
"name" => "$dir$entry",
          
"type" => mime_content_type("$dir$entry"),
          
"size" => filesize("$dir$entry"),
          
"lastmod" => filemtime("$dir$entry")
        );
      }
    }
    
$d->close();
    return 
$retval;
  }
?>

<?PHP
  $dirlist 
getFileList("./categories/");
// echo "<ul class=\"sub-menu\">\n";
  
foreach($dirlist as $file) {
echo 
"<li class=\"menu-item menu-item-type-taxonomy menu-item-object-category\">\n";
    if(
preg_match("/\index.php$/"$file['name'])) continue;
    echo 
"<a href=\"/{$file['name']}\">",basename($file['name']),"</a>\n";
echo 
"</li>\n";
  }
//echo "</ul>\n";
?>

</li></ul></div>    
</nav><!-- #site-navigation -->
</header><!-- #masthead -->

<!-- Open Divs -->
<div id="main" class="wrapper">
<div id="primary" class="site-content">
<div id="content" role="main">

The footer.php

<p>&nbsp;</p>
<p>&nbsp;</p>
<p> 2017 dspl.ca end of file. </p>
<!-- Close Divs -->
</div><!-- .entry-content -->
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main .wrapper -->
</div><!-- #page -->
<p>&nbsp;</p>
</body>
</html>

Now to get into the categories folder

So there is a folder called categories, with all of the subjects we want to show in the menu. Any pages in the root of the site will get the categories menu across the top.

Next. Looking into a category

Let's take a look inside the first directory, Camp

Each folder in categories contains a index.php that has a structure like categories/Camp/index.php below.

<?php include("../../headercat.php"); ?>
<h1>Camping</h1>
<p>Camping, Fishing, Hiking, Biking, Boating, and Skipping Rocks</p>
<?php include("../../cat.php"); ?>

<?php include("../../footer.php"); ?>

It uses a special menu called headercat.php that displays links back to our home pages, and a link back to categories. It then calls on cat.php which displays all of the pages in that directory, using the code modified in a different way fom Chirp. I added a way for it to read the first line from each file from a date stamp so it would display them based on their post date.

headercat.php

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Dspl.ca</title>
<link rel='stylesheet' href='../../css/style.css' type='text/css' media='all' />
<style type="text/css" id="custom-background-css">
body.custom-background { background-image: url('../../bg/bg.jpg'); background-repeat: no-repeat; background-position: top left; background-attachment: fixed; background-size:100%;}
</style>
</head>

<body class="page page-template-default custom-background full-width">
<div id="titlediv"><h1 id="title" title="Hocus Pocus Black Magic"><a href="http://dspl.ca/">Dspl.ca</a></h1></div>
<div id="page" class="hfeed site">
<header id="masthead" class="site-header" role="banner">

<!-- #site-navigation -->
<nav id="site-navigation" class="main-navigation" role="navigation">
            <div class="menu-menu1-container"><ul id="menu-menu1" class="nav-menu"><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/cori/">Cori&#8217;s Stuff</a></li><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/j/">J&#8217;s stuff</a></li><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/anna/">Anna&#8217;s Stuff</a></li><li id="menu-item-2915" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2915"><a href="http://dspl.ca/">Back to Categories >></a></li>
</ul></div>    
</nav><!-- #site-navigation -->
</header><!-- #masthead -->

<!-- Open Divs -->
<div id="main" class="wrapper">
<div id="primary" class="site-content">
<div id="content" role="main">

cat.php

<?PHP 
 
function getPostDate($url) {
 
$data file_get_contents($url);
$date preg_match('/<!--date=(.*?) -->/ims'$data$matches) ? $matches[1] : null;
    return 
$date;
}
 function 
getTitle($url) {
 
$data file_get_contents($url);
//$title = preg_match('/<!--title=(.*?) -->/ims', $data, $matches) ? $matches[1] : null;
    
$title preg_match('/<h1[^>]*>(.*?)<\/h1>/ims'$data$matches) ? $matches[1] : null;
    return 
$title;
}
  function 
sortByTimestamp$a$b ) {
    if (
$a'day' ] == $b'day' ]) {
        return 
0;
    }
    return (
$a'day' ] > $b'day' ]) ? -1;
}

// Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.
  
function getFileListcat($dir)
  {
    
// array to hold return value
    
$retval = array();
    
// add trailing slash if missing
    
if(substr($dir, -1) != "/"$dir .= "/";
    
// open pointer to directory and read list of files
    
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
    while(
false !== ($entry $d->read())) {
      
// skip hidden files
      
if($entry[0] == ".") continue;
      if(
is_dir("$dir$entry")) {
        
$retval[] = array(
          
"name" => "$dir$entry/",
          
"type" => filetype("$dir$entry"),
          
"size" => 0,
          
"lastmod" => filemtime("$dir$entry")
        );
      } elseif(
is_readable("$dir$entry")) {
        
$retval[] = array(
          
"name" => "$dir$entry",
          
"type" => mime_content_type("$dir$entry"),
          
"size" => filesize("$dir$entry"),
          
"lastmod" => filemtime("$dir$entry"),
    
// added
    
"tit" => getTitle("$dir$entry"),
    
"day" => getPostDate("$dir$entry")
        );
      }
    }
    
$d->close();
usort$retval'sortByTimestamp' );
    return 
$retval;
  }
?>

<?PHP
  $dirlist 
getFileListcat("./");
echo 
"<ul class=\"sub-menu\">\n";
  foreach(
$dirlist as $file) {
echo 
"<li class=\"menu-item menu-item-type-taxonomy menu-item-object-category\">\n";
    if(
preg_match("/\index.php$/"$file['name'])) continue;
    echo 
"<a href=\"{$file['name']}\">",basename($file['tit']),"</a>\n";
echo 
"</li>\n";
  }
echo 
"</ul>\n";
?>

The last piece of the puzzle. Adding posts

At this point, you just start adding content. Add folders (as long as you add the index.php that looks like this)

Each folder needs a index.php to land on. Once again, here's the one from categories/Camp

<?php include("../../headercat.php"); ?>
<h1>Camping</h1>
<p>Camping, Fishing, Hiking, Biking, Boating, and Skipping Rocks</p>
<?php include("../../cat.php"); ?>

<?php include("../../footer.php"); ?>

and then add each post in each category as Name_of_Post.php using the same Name_of_Post for the title and using the <!--date=20141027 --> date stamp, headercat.php and footer.php includes like shown below.

<!--date=20141027 -->

<?php include("../../headercat.php"); ?>
<h1>Your title</h1>
<p>Content of your post.</p>
<?php include("../../footer.php"); ?>

That's it! I'll try and get a zip file working one day. Like I said, casual computer user ;) Cheers.

 

 

2017 dspl.ca end of file.