[Tut] How to Force Content?
Welcome to the first tutorial by matt.developedthe.net, this tutorial will be a very basic one to help all you newcomers (annd maybe some vets.) How to force content into any type of page.
For this tutorial I will be using:
- PHP 5
- Htaccess Files
- Apache
- CSS
This is not really a hard tutorial to pick up, but can be confusing if the server your working on is not setup properly.
So lets start by describing the file that we will be creating.
- .htaccess – This will allow us to automatically force content into a page.
- prepend.php – This page will be placed before the content.
- append.phh – This will allow us to alter the content and modify it to allow us to put content in.
First things first the .HTACCESS
This is pretty much a straight forward file, all we need to do in the file is tell the server that you want to pass certain file type (in this case PHP and HTML) to your script.
.HTACCESS
php_value output_buffering 4098
AddType application/x-httpd-php php
AddType application/x-httpd-php html
AddType application/x-httpd-php htm
php_value auto_prepend_file 'd:/htdocs/prepend.php' # replace with your path to the file
php_value auto_append_file 'd:/htdocs/append.php''# replace with your path to the file
Right so I will step througth this code to give you more detail about it.
php_value output_buffering 4098 This will open the buffer
Next we add the file types we want the server to edit
AddType application/x-httpd-php php………
…………….ETC
Then It’s just a case of telling where the prepend file is, and the append file is. The reason will become apparent later on why we use both append and prepend.
Next is the prepend.php this will open the php buffer and take the content ready to be altered.
prepend.php
<?php ob_start(); ?>
Pretty small eh? this is because as said earlier we only need this file to open the buffer.
Last but not least so that the stored data can be altered and printed back onto the screen we need append.php
append.php
<?php $str = ob_get_clean();
$ad = ("<body style='margin:0px;'>\n<a href='http://matt.developedthe.net/'><div style='background-color: #000; width:100%; height:25px; text-align:left; border-bottom: 1px #aeaeae solid;'><img src='http://matt.developedthe.net/images/advert.jpg' alt='part of wheres my tv network' style='border:0px;'/></div></a>");
if ((! strstr($str,$ad)) && (strstr($str,"<body>")))
$str= str_replace("<body>",$ad,$str);Echo($str); Ob_end_flush(); ?>
As you can see this will search through the stored data, to check this data as not already been added and for the <body> tag if it is present it will then alter the data to add the banner in, then output it out onto the page. You can now see this is why both append and prepend is needed beacuse it will make sure the page has a body tag in it to make sure that it does not produce and invalid text that will show up on the W3C check, so if the page was orignally valid it will stays that way
You can however change the search tag by altering $ad , or the “<body>” -(! strstr($str,<altered>)) && (strstr($str,<Search>)to anything you want as long as that is in the page it will find it, just remember that you will also need to alter this bit $str= str_replace(<Search>,<altered>,$str); to then go through and replace the data
that’s it you will now have forced content in your pages.
This tutorial is only here to give you the basic idea on what to do, this method will not work with all sorts of php files (such as php that is purley GD) , or if the the orginal file uses a buffer. This can be taken much much further, so if any of you manage to produce different version leave me a comment, and I will try to amend / add to this tutorial with full credit of course!
Well I hoped you enjoyed this first tutorial and hopefully will be more quite soon.
Thanks matt.developedthe.net
Popularity: 41% [?]
September 9, 2008 - 2:15 pm
Hm .. cool! Here’s how else can play with php!
September 30, 2008 - 4:23 pm
Great tutorial. Thanks
April 30, 2009 - 8:28 am
Sweet, just what I was looking for.
June 16, 2009 - 4:23 pm
i tried this code, but on e107 and many CMS what uses OB function, the site was not working fine
do you have some solution or idea ? :/