The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 06:57:41 AM

Login with username, password and session length


Pages: [1] 2 3
  Print  
Author Topic: Site Example: Straight HTML to Dynamic PHP  (Read 11946 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: September 13, 2007, 01:43:39 PM »

I've received a lot of PMs and seen a lot of posts about folks wanting to do more powerful things with PHP, and since NutBalls is on the path to salvation learning PHP as well, I figgered now would be a great time to do a demonstration thread on what a completely dynamic PHP site might look like.

The client that I'm using as an example came to me with a reasonably attractive, but inconsistent and completely HTML file based site. The code was horribly FrontPaged and the JS was ... well, it needed some work. The client and I decided that a consistent look for the site was required, better navs, a couple changes to the galleries (he is an artist) and wants to do a couple updates per ear. Since I don't like tooling about in crappy HTML, I decided to give away a couple hours of my time and convert the site completely to a PHP, theme-based site. The graphics and "basic feel" of the site were not to be changed.

Essentially, rather than being a hodge-podge collection of HTML, CSS and JS files, it is now a web application - even though it looks simply like a regular website. The way that it is built gives me a centralized point of entry and exit for every page... giving rise to the possibility of doing "something" for every single page call ie., surfer tracking, cloaking if that's your bag, on the fly language translation, shopping cart control... you name it, this is the first step in getting towards that direction.

From an SEO perspective, I don't want or need anyone seeing that I use PHP, nor the shape and structure of my site, nor any GET parameters - which only opens the door for XSS and SQL injection attacks - even if there's no way someone could get in, it doesn't mean they won't try. So I have the .HTML url converted in Apache to a call to a single script, and the original URL is passed as a GET parameter to me. I can then do anything I want to with it, because I perceive the main.php function to be like a single function call, and the GET parameters to be kind of like function parameters that I am responding to. This makes for a pretty simply programmatic structure and is easy to maintain.

Additionally, you will see in this example the notion of using a theme - I like separating the header and footer from the content. This makes it easy to modify a single file and all pages on the website are modified instantly. Although there are many, many ways to do this, I will present here an easy way that I use often.

There will be opinions about the right way and better ways... I get that. If you have suggestions that's completely cool - I'm only presenting one way to spur the imaginations of n00bs and those that haven't done a bunch of this, so they can get an idea of what this kind of site from a back-end or code perspective looks like. There will almost certainly be thoughts about why I blend traditional table layout with CSS work - this is because, IME, a combination of the LCD of both creates the most robust and stable websites regardless of browser.

A munged version of the new site can be seen here:

http://demo1-after.perkiset.org

You may look at it for reference as to what is going on.
« Last Edit: September 13, 2007, 02:03:58 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: September 13, 2007, 01:45:43 PM »

The first thing that needs to be done is to have Apache rewrite the URL so that requests for any number of pages (or even incorrect ones) come into my central script instead. I use VirtualHost directives inside of the httpd.conf file, but this work can be done with a tiny amount of modification through the .htaccess file as well.

The original VirtualHost section looked like this (it has since been removed):
Code:
<VirtualHost 1.2.3.4:80>
        ServerName      demo1-before.perkiset.org
        DocumentRoot    /www/sites/perkiset/demo1-before
</VirtualHost>

The new one looks like this:
Code:
<VirtualHost 1.2.3.4:80>
        ServerName      demo1-after.perkiset.org
        DocumentRoot    /www/sites/perkiset/demo1-after

        RewriteEngine   on
        RewriteCond     %{REQUEST_URI}  /graphics       [OR]
        RewriteCond     %{REQUEST_URI}  /javascript
        RewriteRule     ^(.*)$  -       [L]

        RewriteRule     ^(.*)$  /main.php?uri=$1
</VirtualHost>

The most important change is the mod_rewrite section. The steps as you see them are:
* Turn the rewriting engine on
* Condition: If the requested URI looks like "*/graphics*" OR
* Condition: if the requested URI looks like "*/javascript*" THEN
* RULE: Don't do anything to the request, and stop rewriting.
* RULE: Since there are no conditions, this will always be executed:
rewrite the entire URL from "/afile.html" (for example) to "/main.php?uri=/afile.html"

That's it! Now every single request, even it is a page that doesn't exist will get shoved into main.php for me to handle.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: September 13, 2007, 01:46:50 PM »

My website structures pretty much look the same all the time, regardless of application or client.
Here is a look at the /www/sites/perkiset/demos/demo1-after directory, then the pages directory and the theme directory:


* demodir.gif (1.73 KB, 83x91 - viewed 387 times.)

* pagesdir.gif (3.57 KB, 144x201 - viewed 385 times.)

* themedir.gif (0.65 KB, 100x72 - viewed 392 times.)
« Last Edit: September 13, 2007, 03:31:40 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: September 13, 2007, 01:52:23 PM »

Code:
<?php

// Needed vars...
$thisDir '/www/sites/perkiset/demos/demo1-after';
$pages 'pages';
$pagePath "$thisDir/pages";
$content = array();

// Build Translation table...
$transArr['/'] = $transArr['/index.html'] = 'index.php';
$transArr['/cars.html'] = $transArr['/cars/cars-2.html'] = 'cars.php';
$transArr['/diners.html'] = $transArr['/diners/diners.html'] = 'diners.php';
$transArr['/figures.html'] = $transArr['/figures/figures.html'] = 'figures.php';
$transArr['/jungle.html'] = $transArr['/jungle/jungle.html'] = 'jungle.php';
$transArr['/misc.html'] = $transArr['/miscellany/miscellaney.html'] = 'misc.php';
$transArr['/marbles.html'] = $transArr['/marbles/marbles.html'] = 'marbles.php';
$transArr['/rocks.html'] = $transArr['/rocks/rocks.html'] = 'rocks.php';
$transArr['/contact.html'] = $transArr['/contact_page/contact.html'] = 'contact.php';
$transArr['/bio.html'] = $transArr['/artist_bio/bio.html'] = 'bio.php';

// Get the header/footer...
require("$thisDir/theme/header.php");

// Attempt translation - if not, then return a not found page...
$inPath $transArr[$_GET['uri']];
$fullPath "$pagePath/$inPath";
if ((!
$inPath) || (!file_exists($fullPath)))
{
header("HTTP/1.0 404 Not Found");
echo $header'<br><h3>Unfortunately that page is no longer available.<br>'
'Please <a href="/index.html" class="arial">CLICK HERE</a> to return to our home page.<br><br>'
$footer;
exit;
}

// Get the actual data page...
require($fullPath);


// Echo the page...
echo $headerimplode(chr(10), $content), $footer;

?>


Let's look at that step by step:

First step, obviously, define the vars that I'll be needing.

Second, the transation table.
The translation table is where I convert inbound URLs into actual PHP files that I will be using. Essentially I will use an array of possible inbound URLs as my array indicies and the php file that I want to use as the value. Note how both $transArr['/'] and $transArr['/index.html'] both get converted to 'index.php' - this helps be gather older URLs that might be cached at Google, or simply multiple ways that I might want a surfer to be able to get to a page. The $this = $that = $another form is simply a handy one-line way of assigning values.

Next I get the theme information. The header file looks like this:
Code:
<?php

$year 
date('Y'time());

$header = <<<HEADER
<title>Demo by Perkiset</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<style type="text/css"> 
.arial { font-family: Arial, Helvetica, sans-serif; font-weight: normal; color: #ffffff; }
.bold { font-weight: bold; }
.s10 { font-size: 10px; }
.s11 { font-size: 11px; }
.s12 { font-size: 12px; }
.s14 { font-size: 14px; }
a.arial { text-decoration: none; }
a.arial:HOVER { background-color: #202020; }
</style>

</HEAD>
<BODY bgcolor="#363636" text="#FFFFFF" leftmargin="0" marginwidth="0">
<script src="/javascript/clientBase.js"></script>

<center>
<table cellpadding="0" cellspacing="0" border="0">
<tr><td><img src="/graphics/dot_clear.gif" height="4" width="1"></td></tr>
<tr><td><a href="/index.html"><img src="/graphics/Signature.jpg" width="803" height="99" border="0"></a></td></tr>
<tr><td><img src="/graphics/dot_clear.gif" height="12" width="1"></td></tr>
<tr><td align="center">
HEADER;

$footer = <<<FOOTER
</td></tr>
<tr><td><img src="/graphics/dot_clear.gif" height="15" width="1"></td></tr>
<tr><td><img src="/graphics/stripe.jpg" height="20" width="803"></td></tr>
<tr><td><img src="/graphics/dot_clear.gif" height="10" width="1"></td></tr>
<tr><td align="center" nowrap>
<font size="3">
<a href="/" class="arial bold">&nbsp;home&nbsp;</a>
<img src="/graphics/dot_clear.gif" height="1" width="20">
<a href="/contact.html" class="arial bold">&nbsp;studio & contact&nbsp;</a>
<img src="/graphics/dot_clear.gif" height="1" width="20">
<a href="/bio.html" class="arial bold">&nbsp;artist bio&nbsp;</a>
<img src="/graphics/dot_clear.gif" height="1" width="20">
<a href="mailto:perk@perkiset.org" class="arial bold">&nbsp;email&nbsp;</a>
</font>
</td></tr>
<tr><td><img src="/graphics/dot_clear.gif" height="20" width="1"></td></tr>
<tr><td class="arial" align="center"><font size="2">
Code: Copyright &copy; Perkiset, 2002-
$year<br>
Art: Copyright &copy; Someone else, but the copyright is equally important: 2002-
$year<br>
Please don't scoff or scrape this content. Please.<br>
All Rights Reserved
</font></td></tr>
</body></html>
FOOTER;

?>


It is really only responsible for two variables: $header and $footer. Note that the clientBase.js is simply one of my stock "handy function" javascript files that I often include with sites by default.

My next step is to attempt to convert the inbound URL into a working file name where I can go get the body-content of this page (reprint from above):
Code:
// Attempt translation - if not, then return a not found page...
$inPath = $transArr[$_GET['uri']];
$fullPath = "$pagePath/$inPath";
if ((!$inPath) || (!file_exists($fullPath)))
{
echo $header, '<br><h3>Unfortunately that page is no longer available.<br>',
'Please <a href="/index.html" class="arial">CLICK HERE</a> to return to our home page.<br><br>',
$footer;
exit;
}

// Get the actual data page...
require($fullPath);

As you can see, I simply see if the inbound URL is an entry in my translation table - if it is, require the file - if it isn't, I send back a nice looking 404 page that is within the framework of the site - a much prettier way of doing things than simply "404 Page not found." You can try it with this unknown URL:

http://demo1-after.perkiset.org/forcefail.html

Once the actual content has been included, I echo the whole mess out:
Code:
// Echo the page...
echo $header, implode(chr(10), $content), $footer;

And that's it! Next post, one of the content pages.
« Last Edit: September 13, 2007, 02:07:29 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: September 13, 2007, 01:59:10 PM »

Here is the content of the "Miscellany" page (You may want to expand the size of your browser window if the code is getting cut off, or better yet, go to the demo site and grab the source from there)

Code:
<?php
$previousGallery 
'/jungle.html';
$nextGallery '/marbles.html';
$galleryName 'misc';
$default 'setFlash(1)';
include(
"$pagePath/galleryHeader.php");

$content[] = $gHeader;
$content[] = <<<HTML
<table cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td valign="middle"><div id="detail"></div></td>
<td><img src="/graphics/dot_clear.gif" height="500" width="30"></td>
<td>
<table cellpadding="0" cellspacing="0" border="0">
<tr><td colspan="5"><img src="/graphics/Misc_Header.jpg" height="36" width="273"></td></tr>
<tr><td colspan="5"><img src="/graphics/dot_clear.gif" height="5"></td></tr>
<tr><td colspan="5" onMouseOver="highlightCell(this, '#202020')" onMouseOut="highlightCell(this, 'transparent')" onClick="setFlash(0)">
<div class="arial"><font size="3">&nbsp;<b>Miscellaney</b> is a word...</font></div>
</td></tr>
<tr><td colspan="5"><img src="/graphics/dot_clear.gif" height="15"></td></tr>
<tr>
<td><img src="/graphics/thumb/misc-01_thm.jpg" class="thumb" onClick="setFlash(1)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-02_thm.jpg" class="thumb" onClick="setImage(0)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-03_thm.jpg" class="thumb" onClick="setFlash(2)"></td>
</tr>
<tr><td colspan="5"><img src="/graphics/dot_clear.gif" class="vspace"></td></tr>
<tr>
<td><img src="/graphics/thumb/misc-04_thm.jpg" class="thumb" onClick="setImage(1)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-05_thm.jpg" class="thumb" onClick="setImage(2)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-06_thm.jpg" class="thumb" onClick="setImage(3)"></td>
</tr>
<tr><td colspan="5"><img src="/graphics/dot_clear.gif" class="vspace"></td></tr>
<tr>
<td><img src="/graphics/thumb/misc-07_thm.jpg" class="thumb" onClick="setImage(4)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-08_thm.jpg" class="thumb" onClick="setFlash(3)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-09_thm.jpg" class="thumb" onClick="setImage(5)"></td>
</tr>
<tr><td colspan="5"><img src="/graphics/dot_clear.gif" class="vspace"></td></tr>
<tr>
<td><img src="/graphics/thumb/misc-10_thm.jpg" class="thumb" onClick="setImage(6)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-11_thm.jpg" class="thumb" onClick="setFlash(4)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/misc-12_thm.jpg" class="thumb" onClick="setFlash(5)"></td>
</tr>
<tr><td colspan="5"><img src="/graphics/dot_clear.gif" class="vspace"></td></tr>
<tr>
<td><img src="/graphics/thumb/misc-13_thm.jpg" class="thumb" onClick="setImage(7)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/thumb/plow.jpg" class="thumb" onClick="setImage(8)"></td>
<td><img src="/graphics/dot_clear.gif" class="hspace"></td>
<td><img src="/graphics/dot_clear.gif" height="74" width="74"></td>
</tr>
</table>
</td>
</tr>
</table>

<script>
images[0] = new Image(500, 148);
images[0].src = '/graphics/misc-02.jpg';
images[1] = new Image(500, 375);
images[1].src = '/graphics/misc-04.jpg';
images[2] = new Image(500, 375);
images[2].src = '/graphics/misc-05.jpg';
images[3] = new Image(500, 331);
images[3].src = '/graphics/misc-06.jpg';
images[4] = new Image(500, 497);
images[4].src = '/graphics/misc-07.jpg';
images[5] = new Image(500, 371);
images[5].src = '/graphics/misc-09.jpg';
images[6] = new Image(249, 500);
images[6].src = '/graphics/misc-10.jpg';
images[7] = new Image(371, 500);
images[7].src = '/graphics/misc-13.jpg';
images[8] = new Image(500, 397);
images[8].src = '/graphics/plow_full.jpg';

flash[0] = '<div style="text-align:left; width: 90%" class="arial"><font size="3"><b>Text Removed</b><br><br>' +
'The content of this area has been removed' +
'<br><img src="/graphics/dot_clear.gif" height="150" width="1">';

flash[1] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="misc-01" align="middle">' +
'<param name="movie" value="/graphics/misc-01.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#363636" />' +
'<embed src="/graphics/misc-01.swf" quality="high" bgcolor="#363636" width="500" height="500" name="misc-01" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
'</object>';

flash[2] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="misc-03" align="middle">' +
'<param name="movie" value="/graphics/misc-03.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#363636" />' +
'<embed src="/graphics/misc-03.swf" quality="high" bgcolor="#363636" width="500" height="500" name="misc-03" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
'</object>';

flash[3] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="misc-08" align="middle">' +
'<param name="movie" value="/graphics/misc-08.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#363636" />' +
'<embed src="/graphics/misc-08.swf" quality="high" bgcolor="#363636" width="500" height="500" name="misc-08" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
'</object>';

flash[4] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="misc-011" align="middle">' +
'<param name="movie" value="/graphics/misc-011.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#363636" />' +
'<embed src="/graphics/misc-011.swf" quality="high" bgcolor="#363636" width="500" height="500" name="misc-011" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
'</object>';

flash[5] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="misc-12" align="middle">' +
'<param name="movie" value="/graphics/misc-12.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#363636" />' +
'<embed src="/graphics/misc-12.swf" quality="high" bgcolor="#363636" width="500" height="500" name="misc-12" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
'</object>';

</script>
HTML;

$content[] = $gFooter;

?>


Examination of the Javascript is beyond the scope of this article, but feel free to post questions about it if you wish.

The 2 essential elements of this file are the inclusion of the "galleryHeader.php" file, which contains the nav and look for the galleries, and the gallery content itself. As I'm sure you are starting to see, working in a hierarichy of files will make my work really easy if the client wants to change the look of all galleries... or the look of the header. The galleryHeader.php file looks like this:

Code:
<?php

$gHeader 
= <<<HTML
<style>
#detail { width: 500px; text-align: center; }
.thumb { cursor: hand; height: 74px; width: 74px; }
.hspace { height: 1px; width: 22px; }
.vspace { height: 15px; width: 1px; }
.invis { display: none; }
</style>

<script>
function setImage(idx)
{
document.getElementById('detail').innerHTML = '<img id="detailImage" src="/graphics/dot_clear.gif" height="' + images[idx].height + '" width="' + images[idx].width + '">';
setTimeout("document.getElementById('detailImage').src = images[" + idx + "].src", 10);
storage.storeItem('
$galleryName', 'setImage(' + idx + ')');
}
function setFlash(idx) 

document.getElementById('detail').innerHTML = flash[idx]; 
storage.storeItem('
$galleryName', 'setFlash(' + idx + ')');
}
images = new Array();
flash = new Array();
</script>
HTML;

$gFooter = <<<HTML
<br><br>
<table cellpadding="0" cellspacing="0" border="0"><tr><valign="middle">
<td><a href="
$previousGallery"><img src="/graphics/l_arrow.gif" height="13" width="13" border="0"></a></td>
<td><a href="
$previousGallery"><img src="/graphics/dot_clear.gif" height="1" width="3" border="0"></a></td>
<td><a href="
$previousGallery" class="arial"><font size="3">&nbsp;Previous Gallery&nbsp;</font></a></td>
<td><img src="/graphics/dot_clear.gif" height="1" width="50"></td>
<td><a href="/index.html"><img src="/graphics/u_arrow.gif" height="13" width="13" border="0"></a></td>
<td><a href="/index.html"><img src="/graphics/dot_clear.gif" height="1" width="3" border="0"></a></td>
<td><a href="/index.html" class="arial s14"><font size="3">&nbsp;Home&nbsp;</font></a></td>
<td><img src="/graphics/dot_clear.gif" height="1" width="50"></td>
<td><a href="
$nextGallery" class="arial s14"><font size="3">&nbsp;Next Gallery&nbsp;</font></a></td>
<td><a href="
$nextGallery"><img src="/graphics/dot_clear.gif" height="1" width="3" border="0"></a></td>
<td><a href="
$nextGallery"><img src="/graphics/r_arrow.gif" height="13" width="13" border="0"></a></td>
</tr></table>
<script>
storage = new localStorage();
storage.fileName = 'gallery';
jumpTo = storage.retrieveItem('
$galleryName');
if (!jumpTo) { jumpTo = '
$default'; }
setTimeout(jumpTo, 100);
</script>
HTML;

?>


As with the theme header, this file's only responsibility is to populate the gHeader and gFooter variables so that they can be included as content at the end of the misc.php file.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: September 13, 2007, 02:02:08 PM »

Of course this structure is simply a starting point.

When I do something like this the very next step for me is to use my own back-office management warez to create an editing system, translation manager and such for the site... I'll add caching to make the pages super fast and telemetry and tracking reports for the client. This is just way beyond what I wanted to get across today.

As I mentioned in the intro post, this entire exercise is simply to demonstrate one way to create a dynamic, completely PHP site that is both fast and clean, as well as easily maintained. I look forward to any comments.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #6 on: September 13, 2007, 02:05:12 PM »

Great post perk, very good, ill discuss a little what I do maybe get some discussion going:

I used to run everything through a central file, but now I use separate files for each document and include the header and footer manually
Although this requires a bit more code It gives me greater control.

For example in my header I look for a $ptitle variable which stores the page title.  This way I can have unique titles for each page and not have to cache the entire page.  I do this a lot, for meta descriptions and other stuff. 

Perk wont running everything through a central PHP file slow things down? Or is it minimal?

P.s. I always rewrite to .html rather than folders etc, I think it looks much more natural to users
Logged

I am Tyler Durden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: September 13, 2007, 02:17:29 PM »

I used to run everything through a central file, but now I use separate files for each document and include the header and footer manually
Although this requires a bit more code It gives me greater control.
Cool, let's see:

For example in my header I look for a $ptitle variable which stores the page title.  This way I can have unique titles for each page and not have to cache the entire page.  I do this a lot, for meta descriptions and other stuff. 
Ah, excellent catch - here's how I do that. In my for-reals sites, I keep a variable in the $_GLOBAL array, something like:
Code:
$_GLOBAL['pageTitle'] = 'Default Title';
$_GLOBAL['pageKeywords'] = "this that and another';
One of the last steps in my for-reals sites is to do a simple str_replace of everything in the final output content that could have been modified by <the page that was called>. This again leaves me with central control, but every page has the capability (although not the requirement) to modify those high level items.

Perk wont running everything through a central PHP file slow things down? Or is it minimal?
There is no speed penalty for running through this script or that one... if you're into script mode you're going to take the processing hit no matter what. The addition of a few lines of code to centralize it really won't impact that much. That being said, however, I am about efficiency and speed. So I use the APC cache for two things:

1) code precompile caching - it's automatic with APC and just makes a website BLAZE.

2) See my translation table? Consider for a moment that I can have all kinds of "parameters" on the translated URL - for example, I'll add "cached=true" to pages that I want cached. Then before I translate, I'll look to see if there is a completed version of the requested page in my personal cache - if there is, then I throw that back out and exit the script - it's monstrously fast. If it isn't there (either I didn't want it cached, or it's never been called, or it's expired, or I manually killed it because I changed it) then the entire process is run through, a new page is created, and if I see "cached=true" in the GET parameters then I'll throw it into the APC cache as my last act on the script. Voila, next call for the page it will be cached and wicked fast.

P.s. I always rewrite to .html rather than folders etc, I think it looks much more natural to users
Completely agree - and although I have ZERO evidence of this, I still think that certain engines like flat .html files better than dynamic ones...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
jairez
Expert
****
Offline Offline

Posts: 164


JTFC


View Profile
« Reply #8 on: September 13, 2007, 05:05:52 PM »

Totally cool mod on the httpd.conf that will solve a problem I've only recently seen in people trying to hack mysites because I do indeed run everything through a single file (index.php?page=xxxxxx) and the code simply includes the .html file for viewing.  I actually learned this from a ColdFusion buddy of mine who uses the FuseBox framework to accomplish basically the same thing.  The plus for me in doing this is my clients can use whatever editor they're comfortable with (Dreamweaver, MM Contribute ... etc.) and even though it throws out butchered HTML, I don't really care as it won't effect the site's functionality - by and large anyway.

This is elegant and I'm going to give it a whirl.

Thanks, Perk.  Once again, very nicely done.


btw - my post #2 - Hey, this stuff's easy Nerd
Logged

Spontaneity has it's time and place.  [Sluggo, 1990-ish]
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: September 13, 2007, 05:17:36 PM »

Totally cool mod on the httpd.conf that will solve a problem
mod_rewrite is the shizz.

The good news is that it's as flexible and configurable as sendmail.  Smiley
The bad news is that it's as flexible and configurable as sendmail.   Undecided

Once you start getting a hold of what all can be done you won't hardly be able to imagine sites without it. If you want, start a new thread with problems/challenges you're seeing that you think mod_rewrite might be able to work with and let's take a crack at it. I'm not kidding, it (in combination with regular expressions) may be the most important tool you put in your webmaster superduper utility belt.


btw - my post #2 - Hey, this stuff's easy Nerd
See that now? I remember you saying that about my first Ogoshi. Say... didn't we break one of my toes or dislocate my shoulder on that one?  ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
jairez
Expert
****
Offline Offline

Posts: 164


JTFC


View Profile
« Reply #10 on: September 13, 2007, 06:44:22 PM »

Deal.  Let me get my sh*t together and I'll start a new thread when I've begun the process.

q:  do you have any experience or thoughts about some of the GPL PHP frameworks (e.g. Zend, CakePHP, FuseBox, ... etc.)?
Logged

Spontaneity has it's time and place.  [Sluggo, 1990-ish]
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: September 13, 2007, 11:57:09 PM »

q:  do you have any experience or thoughts about some of the GPL PHP frameworks (e.g. Zend, CakePHP, FuseBox, ... etc.)?

Only as much experience as I needed to decide that I wanted to write my own  Wink

As you certainly remember, I'm rather a hand-rolled kind of guy, since typically what I want to accomplish is either unorthodox or unusual... but that is no slam against the frameworks... right tool for the job and all...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #12 on: September 14, 2007, 12:58:15 AM »

Gah perk, im all set in my ways, and then u go show an alternative way to do things...and doing a good job of convincing me to try it out.

Now im gonna hav eto make a new site to see how it all comes together lol Smiley
You'd better post more lol
Logged

I am Tyler Durden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #13 on: September 14, 2007, 07:37:10 AM »

Most kind, MM, yet again
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #14 on: September 14, 2007, 10:30:07 AM »

cool post, thanks perk.

This is actually very similar to my standard site structure in ASP. I do it slightly different.

Instead I token replacement instead, and though it might be a bit slower, it also allows for me to only manage 2 files for display purposes. I use a single template page that contains the entire HTML file with tokens for everything I want dynamic, such as #TITLE# #KEYWORDS# #NAV# #CONTENT# etc. The other file is a list of function wrappers for each of the replacements and content elements. I have a single parser function that collects all the tokens, then executes the appropriate replacement function. This also works for dynamic elements. Lets say I wanted to include a list of the last 5 searches on a site in the sidebar. the token would be #PASTSEARCHES-5# an the number can just be changed in the template. My replacement function executes appropriately.

This is a great way to deal with sites that might have different templates for different areas. So I code every page of different type of functionality seperately to make code management more visual. Login.asp register.asp browse.asp etc etc. But I deal with very little display stuff in those pages.

I include the replacement functions, and a global parser functions file which deals with all the consistent tokens like title and keywords. But the CONTENT token, or similar things that might change functionality from page to page, I do in the specific page. I could just do it all in 1 big ass file, but I hate scrolling...


Downside to this method is regex/replace.
upside is your entire HTML template is actually HTML, and complete. None of the header footer separation issues of editing the header and causing errors in the footer that you can't figure out. It also means that you can separate the display management from the code, which in a situation where you have retarded HTML designers monkeying with your shit.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1] 2 3
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!