Display image only if referred from certain URL

I would like to show a specific image to visitors of my site only if they've come from a specific URL (i.e. yahoo etc)

Thanks for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andrej PirmanCommented:

you can script some if() or case() requrements when image is shown, based on visitor's IP, which you get from variable:


for example:

$visitors_IP = $_SERVER["REMOTE_ADDR"];
$good_IP = array("", "", "", "");
if (in_array($visitors_IP, $good_IP)) {
    echo "<img src=\"image.gif\">";

Andrej PirmanCommented:
Hmmm....reading your question second time, I was wondering if you want to filter visitors NOT by visitor's origin, but by which referrer was used.
This is more complicated.

first try this:

echo "you came from " . $HTTP_REFERER;

this variable is NOT generated by every PHP setup, so you depend on it to display referer.
Hello phaygarth,

Try :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
	window.onload = function() {
		// get an array of reference on each picture
		var array_of_img = document.getElementsByTagName("img");
		// parse all pictures to check their src/url
		for( var i=0 ; i<array_of_img.length ; i++) {
			// if the url/src of the image don't start by http://domain or don't have http
			if( (array_of_img[i].src.indexOf('http://filedb.experts-exchange.com')!=0) || (array_of_img[i].src.indexOf('http://')==-1) ) {
				// set the src to a default/empty picture
				array_of_img[i].src = "http://www.lawrence.sd.us/images/nopic.gif";
<img src="http://filedb.experts-exchange.com/incoming/2008/04_w17/19317/logo-Easter.jpg" />
<img src="http://www.google.fr/images/nav_logo8.png" />

Open in new window

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

phaygarthAuthor Commented:
To help clarify my question:

If a visitor arrives on my site via a yahoo (eg) search result, then I would like to display 'image1.jpg' to them.

However, if they come from any other referrer, then I would like to display 'image2.jpg' to them (or no image at all)

Hope this makes sense.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Here is the code:

if (isset($_SERVER['HTTP_REFERER']))
    $parseUrl = parse_url($_SERVER['HTTP_REFERER']);
    if ($parseUrl['host'] == "www.yahoo.com" || $parseUrl['host'] == "yahoo.com")
        //user comes from yahoo.com
<img width="65" height="37" border="0" src="//www.examplesite.com/imageYAHOO.gif" alt="blah">
        //user comes from other site 
<img width="65" height="37" border="0" src="//www.examplesite.com/imageOTHER.gif" alt="blah">
    //user opened directly the page - no referrer set.
    //do something...

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.