How to hide ads on specified php pages?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

How to hide ads on specified php pages?

Post by sbaker »

Hi, I'm trying to hide ads on pages that are accessed directly by typing in or clicked through from another site. A good example would be this http://qikr.co/6hv56 - You will notice when you first look - there are no ads, but then when you click on it you will see ads. However, even on the pages where there are ads, if you simply copy the link address in the browser and paste it, you will notice it shows no ads.

I'm not sure if it's its javascript. Does anyone here have any experience with this? =) Thanks!!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to hide ads on specified php pages?

Post by McInfo »

Code: Select all

$_SERVER['HTTP_REFERER']
HTTP_REFERER is a header field sent by the client. It tells you where the client found the link to your page. If there is no such field, the client came directly to your page and was not referred by another page.
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to hide ads on specified php pages?

Post by sbaker »

Thank you McInfo. How can I use HTTP_REFERER in this case? How do I tell it to show no ads, when coming from another site or accessed with direct link. Do you have a sample code? I'm new to php so I'm trying to learn. I researched online and I came across this below, not sure if that's what is used in http://qikr.co/6hv56

<?php

if ($_SERVER['HTTP_REFERER']="https://www.othersites.com"){
echo "<body onload=\"document.location.href='images-with-no-ads.php'\">";
}

else {
echo "<body onload=\"document.location.href='http://www.mydomain.com/randomimage.jpg'\">";
}

?>

Not sure if this is what I want? Any help is greatly appreciated!! =)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to hide ads on specified php pages?

Post by twinedev »

McInfo wrote:If there is no such field, the client came directly to your page and was not referred by another page.
While in general this will work just wanted to note that the referrer can be blank if the browser doesn't give one, either manually disabling it or running AnitVIrus that blocks it (not such a big problem as it was a few years ago when Norton blocked it by default).

The site listed does appear to be using the referrer for ads, as when I disabled mine, I didn't see any more ads.

-Greg
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to hide ads on specified php pages?

Post by McInfo »

Code: Select all

if (isset($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])) {
    if (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['SERVER_NAME']) {
        echo 'advertisement here';
    }
}
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to hide ads on specified php pages?

Post by twinedev »

A simple example:

Code: Select all

<?php
	if (strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])!==FALSE) {
		echo "DISPLAY AD HERE";
	}
?>
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to hide ads on specified php pages?

Post by sbaker »

You guys are awesome! I will try it and let you know if it works for me. Does this code go in the .htaccess file or on the actual page where the image will be?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to hide ads on specified php pages?

Post by twinedev »

The actual file at the place in the code where you want the ad (so in the example URL you gave, it would be after the image)

-Greg
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to hide ads on specified php pages?

Post by twinedev »

Also, another way to do it if don't want to have to worry about placing it in quotes for echo statements (simplified code here)

Code: Select all

<html>
  <head>
    <title>This is my page</title>
  </head>
  <body>
    <h1>Phantastic Fotos</h1>
    <img src="/img/<?php echo (int)$_GET['id']; ?>.jpg">
    <?php if (strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])!==FALSE): ?>
      <a href="/adhandler.php?ad_id=3">Click here for a fantastic deal!</a>
    <?php endif; ?>
  </body>
</html>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to hide ads on specified php pages?

Post by McInfo »

If a script doesn't check whether an array index exists before using it, PHP generates an "undefined index" notice. Even if the error message isn't visible on the screen, it could be filling up the error log.
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to hide ads on specified php pages?

Post by sbaker »

McInfo wrote:If a script doesn't check whether an array index exists before using it, PHP generates an "undefined index" notice. Even if the error message isn't visible on the screen, it could be filling up the error log.
What can be done to avoid this problem?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to hide ads on specified php pages?

Post by McInfo »

sbaker wrote:What can be done to avoid this problem?
The answer is seven posts away from this one.
sbaker
Forum Newbie
Posts: 12
Joined: Wed Jul 20, 2011 12:47 pm

Re: How to hide ads on specified php pages?

Post by sbaker »

McInfo wrote:

Code: Select all

if (isset($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])) {
    if (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['SERVER_NAME']) {
        echo 'advertisement here';
    }
}
I am using Google AdSenese for advertisement, do I enter the entire AdSense code in echo 'advertisement here'; ?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to hide ads on specified php pages?

Post by McInfo »

You can put the AdSense code there, or in an included file, or in a function that gets called on that line.
Post Reply