-1

Related to Google Display ads, I want an image on several pages to be different if the page is referred from an outside source versus an internal link. So the image source on this one image needs to conditional based on the link source. I presently have 2 versions of these pages but its a bit messy with canonical links etc. The web site uses .php.

This is the line with the image link for pages called internally. Just need to change "src=" for outside referrers.

<img style="float:right;margin:-27px -2px 10px 15px; 
            box-shadow:-6px 11px 30px 0px #a7a7a7;" 
     src="rotate-home/rotate.php?t=<?php echo time(); ?>" 
     width="426" 
     height="236" 
/>
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
swbca
  • 1
  • 2

2 Answers2

1

You may want to use $_SERVER["HTTP_REFERER"], i.e.:

if( isset( $_SERVER["HTTP_REFERER"] ) )
{   
    $referer = $_SERVER["HTTP_REFERER"];
    if ( preg_match('/mydomain\.com/i', $referer) )
    {
        # local referer
    } else {
        # remote referer
    }
} else {
    # no referer
}

Notes:

1 - HTTP_REFERER can be easily spoofed.
2 - <?php echo time(); ?> can be shortened to <?=time()?> on php >= 5.4 or php < 5.4 after enabling short_open_tag on php.ini

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Would it too complicated to show how this logic would be applied to selecting the image src. I am good ad editing code, but not creating it. – swbca May 04 '17 at 13:04
  • That's simple, just change the image src depending on the referer. – Pedro Lobito May 04 '17 at 13:06
0

This answer is from Pedro Lobito, so he should get the credits. I am just refactoring his code.

<img style="float:right;margin:-27px -2px 10px 15px; box-shadow:-6px 11px 30px 0px #a7a7a7;" src="<?php echo getReferer($_SERVER["HTTP_REFERER"]);?>"?t=<?php echo time(); ?>" width="426" height="236" />

<?php
function getReferer($referer){
    if(empty($referer)) return '/image/no-referer.png';
    return preg_match('/mydomain\.com/i', $referer) ? 'image/local-referer.png' : 'image/remote-referer.png';
}
?>
Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33
  • Would it be complicated to illustrate how to apply the results of this logic to loading image A or image B. I can do elaborate good quality code editing in my existing CSS site . . but I don't know how to apply the results to the image load. Thankis – swbca May 05 '17 at 13:53
  • Ok I edited my response. I haven't personally test it, so you might receive some warning or errors. Just make sense of them. – Leandro Papasidero May 05 '17 at 17:23
  • THIANK YOU . . . ITS WORKS! – swbca May 06 '17 at 10:12
  • A Glitch . . the actual image src for the local referrer is a "rotate.php" script to rotate images in the folder . . as shown in your code here. return preg_match('/mygig-disk\.com/i', $referer) ? 'rotate-home/rotate.php?' : 'images/remote-referer.jpg';} – swbca May 06 '17 at 11:08
  • (continue) The rotate.php? script rotates images when refreshing the page, but it does not rotate images when requesting the page from an internal link . . all pages with this code display the same image first loaded. (this follow-up issue may beyond the purpose of this forum. Disregard if appropriate) – swbca May 06 '17 at 11:26