Welcome to WebmasterWorld Guest from 3.231.226.211

Forum Moderators: martinibuster

Message Too Old, No Replies

A Script To NOT Show AdSense on Certain Pages

     
8:01 pm on Jun 16, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 5, 2006
posts:694
votes: 0


Hi. I need to know if there is some kind of a script (that is ok with AdSense TOS) that you can place on certain pages to NOT show AdSense ads?

Those of us who have ecommerce sites that are using templates that come with the ecommerce host we cannot adjust everything like you can when you build your own website.

I don't have the time to add AdSense code to every single page I want it on manually, so I have it added to the template, but that makes it appear on every page and there are some pages I want the ads to NOT show up if that makes sense.

Thanks in advance for any help.

9:11 pm on June 16, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member netmeg is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2005
posts:13012
votes: 222


Depending on the ecommerce software, it'd be just as much work to code that as it would be to just add the code to the pages you want.
10:53 pm on June 16, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 3, 2007
posts:724
votes: 0


If you look at your page structure, you might be able to find an element that exists in the pages that you want to have AdSense, but does not exist in the ecommerce page.

For example: The eCommerce pages might have an area where the shopping cart resides. In the same area of your non-eCommerce pages, maybe the shopping cart is replaced with something else... maybe you can add your AdSense code to the module that displays the "something else."

10:56 pm on June 16, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:June 18, 2005
posts:1868
votes: 90


If you have many pages and it is not really an option to add the code to every one of them, better insert it in the template, and make a few exceptions, ex:

IF PAGE NOT "contact.html" AND NOT "privacy.html" AND NOT "login.thml"
INSERT ADSENSE CODE

8:43 am on June 17, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 26, 2008
posts:46
votes: 0


WolfLover, Koan's idea will work fine if you are only looking to exclude a handful of pages. A quick way to implement this in PHP would be to wrap the following code around your AS ...

<?
$currurl = curPageURL(); // assigns full URL of current page to variable
$currurl = ltrim($currurl, 'https://');
$currurl = ltrim($currurl, 'http://'); // removes URL prefix

if ($currurl != "www.example.com/red-widgets.php" AND $currurl != "www.example.com/white-widgets.php" AND $currurl != "www.example.com/blue-widgets.php")
{
?>
AdSense Code Goes Here
<?
}
?>

A better way is to place the code in a separate file and use REQUIRE as follows ...

1. Add following code to a new file on its own (no headers etc) and save in the root as adsense-urls.php

<?
$currurl = curPageURL();
$currurl = ltrim($currurl, 'https://');
$currurl = ltrim($currurl, 'http://');

if ($currurl != "www.example.com/red-widgets.php" AND $currurl != "www.example.com/white-widgets.php" AND $currurl != "www.example.com/blue-widgets.php")
{ $showas = 1; } // variable that controls whether AS is delivered.
?>

2. In the template replace the AS code with ...

<?
require ($_SERVER['DOCUMENT_ROOT']."/adsense-urls.php");
if ($showas == 1) {
?>
AdSense Code Goes Here
<?
}
?>

With this method, any changes to the excluded URLs can be made in the adsense-urls.php file and have an immediate global affect.

If you are looking to exclude AS from all pages within certain locations, eg www.example.com/help/ and www.example.com/contact/, then you can replace the argument above with ...

if (strpos($currurl, "example.com/help") === FALSE AND strpos($currurl, "example.com/contact") === FALSE)
{ $showas = 1; }

As with all PHP, and especially when using REQUIRE, test off-line first. An error in a REQUIRE file will prevent all pages that call it from loading.

This is a primitive way of controlling the deployment of AdSense, but perfectly effective if you only have a handful of URLs (or paths) that you wish to exclude. For anything more elaborate you should look to storing the criteria in a database. Our AS deployment system doesn't work on URLs, but takes several other factors into account such as page type, visitor location and volume of content before deciding what (if any) ads to display.

Hope this helps.

Dick

1:31 pm on June 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 5, 2006
posts:694
votes: 0


Thank you so much for your help everyone! I will try this out and see if it works. I only need to not show it on a handful of pages like privacy, shopping cart, etc.

I'm pretty good with html but php, I have very limited knowledge about.

Again, you people here are awesome!

3:33 pm on June 17, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 28, 2005
posts:552
votes: 0


It may be that the code is inserted into the template files that are not executed as PHP, but parsed by PHP which would make for a much more difficult programming hack.
3:53 pm on June 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 5, 2006
posts:694
votes: 0


koan, since I do need to only exclude a few pages from showing Adsense, is what you've written something that I would write in HTML? I've never done this before so I am confused!

ThirdWheel, thank you for your reply, I am not sure I can use php with this host. I truly appreciate your help as well!

4:52 pm on June 17, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 26, 2008
posts:46
votes: 0


WolfLover,

Yep, my PHP solution is dependent upon on what your server can run, but I figured I'd be optimistic and post it. Without knowing more about the limitations we have to work with it isn't possible to give you a definitive answer, except maybe javascript, but that wouldn't be my choice in this situation.

A quick fix could be to break the pages away from the template and create straight html files from the final source code. However, that depends on whether the pages run any server side code (best sellers, top 10s etc), and also assumes you have FTP access.

Dick

10:24 am on June 18, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 26, 2008
posts:46
votes: 0


WolfLover,

I realised after my last post that breaking templates won't work for you, and that you probably can't use any server side code on your templates. If that's the case, here's the javascript solution ...

<div id="adsense" style="display: none">
Adsense Code Goes Here
</div>
<script type="text/javascript">
if(document.location.href != "http://www.example.com/red-widgets.htm" && document.location.href != "http://www.example.com/white-widgets.htm" && document.location.href != "http://www.example.com/blue-widgets.htm")
{
document.getElementById('adsense').style.display = '';
}
</script>

Not to claim any undue credit, I've modified code from a previous post by Drag_Racer, [webmasterworld.com...]

As ever, hope this helps.

Dick

11:52 am on June 18, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:June 18, 2005
posts:1868
votes: 90


Hi WolfLover, no I was just expressing the general concept, you'd need to write it with real server side code for it to work, like ThirdWheel explained. But if you have no knowledge of a programming language like PHP (or don't know anyone who can help you), then you'll probably have to find another way.
12:09 pm on June 18, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member play_bach is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 20, 2005
posts:3076
votes: 4


Assuming you have redirect privileges and your software can make friendly URLs (no ? marks in the string) you could also create static HTML files of the pages you don't want AdSense on. Just do a save complete page in Firefox, edit out the AdSense code, upload the page and it's supporting files, then redirect to it.

[edited by: Play_Bach at 12:17 pm (utc) on June 18, 2009]

12:18 pm on June 18, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 26, 2008
posts:46
votes: 0


create static HTML files

That'd be the way I'd go if possible (although server side code for shopping cart's a likely issue), but not sure how flexible these ecommerce packages are - no experience of them here.

I should also point out that the javascript code I posted hides the AdSense code, it doesn't stop the page requesting it. The only way I know how to do that is to insert code between the javascript start/end tags of the G code, and that could be seen as modifying the code, thus in violation of the TOS. Everyone I know who has done this has sought (and received) permission from G. That may not be so easy these days. Maybe someone here knows more or has another way ...

Dick

[edited by: ThirdWheel at 12:28 pm (utc) on June 18, 2009]

12:48 pm on June 18, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member play_bach is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 20, 2005
posts:3076
votes: 4


What would be nice is if this was something you could do in your AdSense account - just list whatever URLs you didn't want AdSense to show on and Google would take care of it on their end.
3:30 pm on June 18, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 5, 2006
posts:694
votes: 0


As usual you all have been very nice and very generous with your time trying to help me. I'm going to contact my host to see if I can implement any of these ideas.

Thanks again!

3:33 pm on June 18, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 5, 2006
posts:694
votes: 0


Play_Bach, that is an AWESOME idea! That would make all of our lives so much easier.