Trying to get url of page my image is displayed on. Ideas???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Trying to get url of page my image is displayed on. Ideas???

Post by idotcom »

Hi there.

I am trying to get the url of the page that is loading my images. I allow people to display images on their pages but loading them from my server.

example

http://www.somedomain.com/thispage.html
This page loads my image like this


<img src="http://www.mydomain.com/images/provide_ ... simage.gif">


The script on my site provides the image and everything works like a charm. But, I need to know how to reliably get the url of the page loading the image.

I tried

$HTTP_REFERER;
$HTTP_SERVER_VARS['HTTP_REFERER'];
$_SERVER['HTTP_REFERER'];
global $HTTP_REFERER;


I try assigning them to a var like $thereferer = $HTTP_REFERER;

Nothing is working... and I need this to work. If anyone could please help me out with this... I would so appreciate it.

Thanks in advance! :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

referrer information is optionally sent, there is no guaranteed way without the URL encoding the information somehow.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the cleanest (imho) solution is quite simple: just start a session on each of yoru pages.... and instead of returning the images directly, place them somwhere out your webroot, and have a script like this:

somepage.php
session_start();
$_SESSION['washere'] = 'ok';

<img src="image.php?path=image_id" />


image.php

session_start();
if (!array_key_exists('washere', $_SESSION))
{
// they are coming from somewhere else
}
else
{
header(' correct content type etc... ');
file_get_contents( path to the image);
}
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

How abt getting the IP by $_SERVER['REMOTE_ADDR'] ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

REMOTE_ADDR has nothing to do with referrer.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

REMOTE_ADDR only gets the ip of the user that is viewing the image not the server it is on.
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post by idotcom »

Thanks guys for your input!!!

Well the problem in my case is that not all users have php pages. they might have a variety such as html, cgi, php, and so on. What i need is to get the referrer (url of page loading my image) with the code in my php page that provides the image.

I cant rely on others pages to provide the data.

there must be a way.... :?

One of you experts know how :D

Thanks again fellers!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there is no absolute way. Only if the browser requesting the page provides referrer information, or they supply referral information in the request.

You could make them request the image from a script, whereby if the referral information isn't supplied (in the request uri) you return a bad image of some sort.. or nothing at all, breaking the image on the calling page.
Post Reply