Dragen Posted September 24, 2007 Share Posted September 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/ Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354857 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 I believe the short answer is no then. You'd need the referring page (img_test.php) to pass the value onto image.php ideally through a get variable. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354868 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 I thought it'd be a no, but I quite need it to be a yes There must be some way of passing it on. Or would it perhaps have to be done in javascript.. if that's possible. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354873 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354879 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354884 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 okay.. well I'm still gonna be testing the javascript to make sure, but thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354901 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 Are you saying that although you can't alter the PHP of img_test.php that you can alter the JavaScript, or are you referring to the JavaScript of the referring page. Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354904 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354910 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354916 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354948 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354956 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354967 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-354991 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 yeah that'd be great, because I just can't get it to work! Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355006 Share on other sites More sharing options...
cooldude832 Posted September 25, 2007 Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355009 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 thanks cooldude, but the referrer is very unlikely to be on my server, so I can't do something like that. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355012 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355047 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Author Share Posted September 25, 2007 that just seems to be tracking the page which contains the image to me... or am I just not looking right. I'm wanting to track which pages link to the page containing the counter. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355307 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355526 Share on other sites More sharing options...
Dragen Posted September 26, 2007 Author Share Posted September 26, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355655 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 Possibly, but when I found the link I thought it was probably what you were after. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355661 Share on other sites More sharing options...
Dragen Posted September 26, 2007 Author Share Posted September 26, 2007 thanks! I'll give it a try and see how it goes, but for now 'topic solved!' Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-355666 Share on other sites More sharing options...
Dragen Posted September 27, 2007 Author Share Posted September 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70548-passing-http_referer-to-another-file-from-file-i-cant-directly-access/#findComment-356352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.