PHP Guide for AdSense Publishers
Yesterday I went public with The HDTV Shoppe as an example of how to integrate AdSense with embedded content that is normally unavailable to the AdSense crawler. Today I'd like to talk about a related topic, how AdSense publishers can use PHP to their advantage to simplify website creation.
PHP is a programming language, but don't let that scare you if you're not technically inclined. The truth is, you don't have to know much to take advantage of some basic PHP features, features that can be extremely useful in your website development. We're limiting ourselves to “plain” websites here, but if you develop a familiarity with PHP you'll find that it becomes a lot easier to customize WordPress blog themes and similar things.
How PHP Works
PHP is what's known as “server-side scripting”. In other words, you mix PHP commands with the HTML tags of a page and when the web server is asked for the web page it first runs the PHP commands and then returns the processed page — not the actual page that's sitting up on the web server. In other words, if you had this page sitting on your web server:
<html> <body> <b><?php echo "Hello, world."; ?></b> </body> </html>
then a request for the page would return:
Hello, world.
PHP commands start with “<?php” and end with “?>”.
Remember, the PHP commands gets processed on the server, not in the browser. This is different from JavaScript code, which runs in the browser. This is why PHP processing is referred to as “server-side”.
Enabling PHP Support
Almost every hosting service these days supports PHP. If you're not sure, contact your service. Normally, PHP commands are only executed for pages that end in “.php”, while pages that end in “.html” or “.htm” are sent down to the browser without any processing. But that's just the default setup. Personally, I prefer to use a “.html” extension with all my files. It looks better and, frankly, it shouldn't matter to the visitor what technology I'm using to process my web pages.
To enable PHP processing for .html files, all you do is add two lines to the .htaccess file for your site:
RemoveHandler .html .htm AddType application/x-httpd-php .php .html .htm
This obscure syntax says: “forget about the normal processing for .html and .htm files; whenever you see a .php, .html, or .htm file run it through the PHP processor”. Now you're ready to have some fun.
Using PHP for Common Page Elements
PHP makes sharing common page elements among different pages quite simple. A “common page element” is something like a header, a footer, or a navigational menu that is shared between two or more pages on the site.
Let's take a footer as an example, because it's so simple. To create a footer, use a text editor to create a file called “footer.php” — use the .php extension for the command page elements and reserve the .html extension for the actual pages. It would look like this:
<div id="footer"> Copyright © Eric Giguere </div>
You may notice that there are no PHP commands in this file. That's because the file is pretty simple. But you could put PHP commands in the file if you wanted.
Now in your page instead of pasting in the code above you just “include” the footer.php file:
<html> <head> <title>A Simple Page</title> </head> <body> <?php include 'header.php'; ?> <p>This is where my content would go.</p> <?php include 'footer.php'; ?> </body> </html>
Actually, the code above also includes a header.php file. So now you can change the footer on every page just by changing the footer.php file, and similarly for the header.
Including AdSense Code
You can probably see where I'm going with this. You don't have to limit yourself to headers and footers. Any block of code that you find yourself repeating over and over throughout your pages can be included via PHP in a similar manner. Such as your AdSense code!
What I do is create a file for each type of ad or link unit I'm using on a site. For example, I might create a largerect.php file with the AdSense code for a large rectangle format ad unit:
<div class="largerect"> <script type="text/javascript"><!-- google_ad_client = "pub-5964030199537728"; google_alternate_color = "FFFFFF"; google_ad_width = 336; google_ad_height = 280; google_ad_format = "336x280_as"; google_ad_type = "text_image"; google_ad_channel ="0373974695"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "206ba2"; google_color_text = "000000"; google_color_url = "000000"; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div>
Then whenever I want to include a large rectangle in the content I just reference the file:
<p>This is some text. I want to place a large rectangle ad unit immediately after it.</p> <?php include 'largerect.php'; ?> <p>There we go.</p>
Now that's pretty easy, isn't it? And if you know what you're doing, you can even fiddle with the AdSense code itself with PHP commands — as long as the end result (what the browser sees) is something that would be generated from the AdSense console. For example, a website owned by two people could use PHP to display one person's publisher ID half of the time and the other person's ID the other half of the time.
What I've shown you above is just the very tip of the iceberg. There are many books and online resources available for budding PHP users. A good place to start is with the official PHP Manual, but I hope this mini-tutorial has helped you.
P.S.: I see today that this blog has finally recovered from the domain renaming I had to do a while back and is showing a PageRank of 6 in most Google datacenters now. (See here to check it yourself.) That's great, but now I need your help getting my pages out of Google's supplemental results and into the main index — a link from you directly to one of the permalink pages in this blog would be much appreciated. I can't do too much about the quality of the spammy links from the MFA blogs that just copy my postings wholesale, unfortunately, except by combating them with quality links from my readers!
Sponsored Link: If you're apprehensive about writing content, I highly recommend you read Turn Words Into Traffic to break the writing process into a set of manageable and not-so-scary steps.
Eric Giguere is the contextual advertising expert who wrote Make Easy Money with Google and Uncommon AdSense. If you like this posting, why not link to his blog or bookmark it as one of your favorites?
| Enjoyed this post? Get free updates by mail or by RSS! |