Jump to content

passing HTTP_REFERER to another file, from file I can't directly access.


Recommended Posts

I'm trying to get the page referrer to a page. I know that HTTP_REFERER is unreliable, but for what I'm doing that doesn't matter.

 

The problem I've got is that the file I'm getting the referrer from is linked to from another file. Basically I have one file containing an image, which is actually a php file that collects certain data.

img_test.php:

<img src="http://localhost/counter/3/img.png" />

Then in the image php file I have:

<?php
if(isset($_SERVER['HTTP_REFERER'])){
return $_SERVER['HTTP_REFERER'];
}else{
return '';
}
?>

The problem is all I'm getting as the referrer is http://localhost/img_test.php. Which I can understand as it's the file linking to the image.

 

Is there some way I can pass on the pages referrer to the image file? I could use php to create a GET variable, but I don't have access to the img_test.php file, because it's for a freeware project that people can use themselves and it's also designed to be used in .html files so less technical minded people can use it..

 

Any help would be greatly appreciated!

Thanks

yeah, just checked the javascript side of doing this and it seems that I may have to use that (although I haven't tested it yet). Which is a shame as some people disallow javascript so it's a lot less reliable than php.

 

If anyone can think of a way for me to work it with php I'd more more than grateful!

There must be some way of passing it on. Or would it perhaps have to be done in javascript.. if that's possible.

 

Nope, the only way to do it would be to edit the img_test.php page, as that's the referring page, regardless of the technology used whether it be client or server side.

 

Regards

Huggie

No i can't alter the content on image_test.php, apart from telling the user what to put as the url to the image. The problem is I'm wanting image to be output using html, not php as many of the users wont have php or wont understand it anyway, so there files will most likely be .html.

 

I'm talking about adding the javascript into the code which creates the image, which will probably give much the same result as using http_referer in that file, but thought it's worth a try.

Is there some way I can pass on the pages referrer to the image file? I could use php to create a GET variable, but I don't have access to the img_test.php file, because it's for a freeware project that people can use themselves and it's also designed to be used in .html files so less technical minded people can use it..

 

Is this something you can show me so I have a better understanding of what you're trying to achive?

 

Regards

Huggie

okay. What I'm creating is a freeware hit counter that collects info of visitors to add to the stats. So I'm trying to get the referrer, so clients can tell who is linking to them.

The client would put the following (or similar) code into their page on their own website:

<img src="http://counterstop.co.uk/counter/3/img.png" border="0" alt="counterstop" title="counterstop" />

Which using htacces re-directs to my counter.php file which creates the image. The part between 'counter/' and '/img.png' in the url is the GET variables, which I extract in my php file.

 

Then in my counter.php file I go through the process of creating and displaying the image depending on the clients preferences (set in a control panel).

Before I create the image I need to collect the data from the current viewer to the client's page. This is the problem. Because the only code I can use has to be done within the counter.php file, meaning that all I get as the referrer is the client's page, instead of the referrer to the client's page.

 

if you go to: http://test.gimppro.co.uk/link.html

you can see what I mean. click on the link and it should display the counter. But it should save the referrer as http://test.gimppro.co.uk/link.html, but it doesn't.

 

 

I hope that made some vague sense..

It does make sense and it's what I thought you were trying to do.  It isn't possible unless they're using a scripting language to pass a referring URL to your script.

 

For a real technical overview, view the http specification

 

Regards

Huggie

yeah.. the thing is I've seen other hit counters do it: http://www.webstat.net/basic/demo.php

You can see on the link above that it tracks referers, and to implement the counter you simply put the imkage code into your web page:

<img src="http://www.webstat.net/basic/track/47247.png" alt="Website Counter" border="0" />

 

So somehow they're working around the problem.

I see what you're after now and you don't need to do anything special.  It should work fine.

 

I've just tested it and it works a treat.

 

Want me to provide you a simple example?

 

Regards

Huggie

Well if the refer is in your server, you can always do a pretty neat trick i found

on each page create a 2 sessions

$secret = rand();

$_SESSINON[$secret] = $page_name;

 

Then when you get to the next page you can return this name for your use, or insert an input type of

<input type="hidden" name="refer_<?php echo $secret;?>" value="<?php $page_name;?>" />

 

Then on the next page return this input (by finding it out with string test) and then compare if that session value exsit and is valid.  This will stop any cURL() because they can't set a session on your server, and even if they make a random number and a valid page name it wll never valid aganist the true session.

I don't have rewrite on, so you'll have to use the following image tag...

 

<img src="http://dizzie.co.uk/tracker/image.php">

 

Place it on your page and see what happens when you refresh the page a few times.  Then access the following url...

 

http://dizzie.co.uk/tracker/stats.php

 

Regards

Huggie

I stand corrected...

 

Take a look at StatCounter.  They use javascript to log the details.  Once you've signed up, they'll give you the code to insert into your page, open the javascript link they send you and see what it contains.

 

Regards

Huggie

thanks! I think I've found the lucky line ;)

one of the first bits of code in the javascript was this:

sc_referer=""+parent.document.referrer;

So I guess it's getting the 'parent' referrer, instead of the page holding the image. I know this has moved from php to javascript now though...

 

You'd think that php would have a similar function to the javascript 'parent.document', type thing, but I guess because it's server side that may be impossible.

hi.

I've been trying the parent.document.referrer, but have found that cookies aren't being set for some reason. If I run the image directly: http://localhost/counter/3/W900/img.png, then it sets the cookie and works as expected, but if I'm simple linking the image from another file:

<img src="http://localhost/counter/3/img.png" border="0" alt="counterstop" title="counterstop" />

Then it seems to refuse to set a cookie for it. I know it's going through the javascript, because it reloads the image with the phpsessid which is done in javascript, but it just wont set cookies. I even tried using an alert, but that does nothing.

I'm going to post in the javascript section as well, but any idea why the javascript inside my php image file wont set, even though it's reading the javascript in the file?

 

Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.