How to hide ads on specified php pages?
Moderator: General Moderators
How to hide ads on specified php pages?
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!!
I'm not sure if it's its javascript. Does anyone here have any experience with this? =) Thanks!!
Re: How to hide ads on specified php pages?
Code: Select all
$_SERVER['HTTP_REFERER']Re: How to hide ads on specified php pages?
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!! =)
<?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!! =)
Re: How to hide ads on specified php pages?
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).McInfo wrote:If there is no such field, the client came directly to your page and was not referred by another page.
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
Re: How to hide ads on specified php pages?
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';
}
}Re: How to hide ads on specified php pages?
A simple example:
Code: Select all
<?php
if (strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST'])!==FALSE) {
echo "DISPLAY AD HERE";
}
?>Re: How to hide ads on specified php pages?
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?
Re: How to hide ads on specified php pages?
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
-Greg
Re: How to hide ads on specified php pages?
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>Re: How to hide ads on specified php pages?
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.
Re: How to hide ads on specified php pages?
What can be done to avoid this problem?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.
Re: How to hide ads on specified php pages?
The answer is seven posts away from this one.sbaker wrote:What can be done to avoid this problem?
Re: How to hide ads on specified php pages?
I am using Google AdSenese for advertisement, do I enter the entire AdSense code in echo 'advertisement here'; ?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'; } }
Re: How to hide ads on specified php pages?
You can put the AdSense code there, or in an included file, or in a function that gets called on that line.