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:
< ?php echo \"Hello, world.\"; ?> l>
t for the page would return:
Hello, world.
PHP commands start with “< ?php" and end with "?>“.
r, 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:
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:
< ?php include 'header.php'; ?> This is where my content would go. < ?php include 'footer.php'; ?>
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:
Whenever I want to include a large rectangle in the content I just reference the file:
This is some text. I want to place a large rectangle ad unit immediately after it. < ?php include 'largerect.php'; ?> There we go.
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.
Originally from An AdSense Blog: Make Easy Money with Google on October 12, 2006, 11:15am
Related Posts