Send a different image from my web host if it was already requested by an IP address?

Send a different image from my web host if it was already requested by an IP address? - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Send a different image from my web host if it was already requested by an IP address? error on your web browser. Problem :


I am hosting an image with my hosting provider host gator and people can request it with www.example.com/image.png


When an IP address requests the image www.example.com/image.png the first time it sends them one image.


If that IP address ever requests that same image again, I want to make it send a different image.


How can I accomplish something like this?


Solution :

You request image.php (not image.png) and ensure the returned result includes the header Content-Type:image/png so it is processed as an image.


You can set the header in php with


header("Content-Type: image/png");

The IP address can be picked up using the apache/environment variable REMOTE_ADDR - in PHP you can get this with $_SERVER["REMOTE_ADDR"]


You can read the content of the appropriate image using file_get_contents - so you might have code like the following pseudocode php code (the bits in the asterisks need to be replaced and depend on your database and how you interact with it:


<?php 

# We want the browser to render this as a PNG image
header("Content-Type: image/png");

# We don't want to cache the output.
header("cache-control: max-age=0, no-cache");
header("expires: Sat, 01 Jan 2000 01:01:01 GMT");

if *$_SERVER["REMOTE_ADDR"] not in database*
echo file_get_contents("/path/to/initial.png");
else
echo file_get_contents("/path/to/repeatfile.png");

*insert $_SERVER["REMOTE_ADDR"] into database*

?>

You would call this with something html code like


<img src="imagemod.php" alt="changing image">

We hope that this article has helped you resolve the redirects, ip-address, image-hosting error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=