[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Display image only if referred from certain URL

Posted on 2010-04-04
6
Medium Priority
?
266 Views
Last Modified: 2012-05-09
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.
0
Comment
Question by:phaygarth
6 Comments
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 29671036
Hi,

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

$_SERVER["REMOTE_ADDR"]

for example:

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

0
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 29671705
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.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 29672039
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">
<head>
<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";
			}
		}
	}
</script>
</head>
<body>
<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" />
</body>
</html>

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:phaygarth
ID: 29906280
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.
0
 
LVL 12

Accepted Solution

by:
jet-black earned 2000 total points
ID: 29939808
0
 
LVL 12

Expert Comment

by:jet-black
ID: 29940040
Here is the code:

<?php
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">
<?php
    }
    else
    {
        //user comes from other site 
    ?>
<img width="65" height="37" border="0" src="//www.examplesite.com/imageOTHER.gif" alt="blah">
<?php
    }
}
else
{
    //user opened directly the page - no referrer set.
    //do something...
}
?>

Open in new window

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question