My site is a video site and I just installed a link dump (phplinkdropper) into a subdirectory of the site. So I have: www.mysite.com - the video part www.mysite.com/link-dump - the link dump part The problem is with the way the link dump script tracks hits in. The link dumps index.php (www.mysite.com/link-dump/index.php) must be called to track the hits in. So say someone arrives at www.mysite.com. No hits are tracked because the tracking code is part of the link dump index. Now if someone arrives at www.mysite.com/link-dump, hits are tracked just fine. So what I did was put the tracking code in its own php file and include it whenever www.mysite.com loads. Below is the file I made("http://www.mysite.com/link-dump/plugs.php"): The problem is that $_SERVER['HTTP_REFERER'] is "" while in this file. Both before and after the include, $_SERVER['HTTP_REFERER'] is setup perfectly. So: echo $_SERVER['HTTP_REFERER']; works perfectly include ("http://www.mysite.com/link-dump/plugs.php"); <-- while in here it doesn't work echo $_SERVER['HTTP_REFERER']; works perfectly <?php include ("config.php"); include ("functions.php"); $referral = $_SERVER['HTTP_REFERER']; $array=parse_url($referral); $referral1 = $array['host']; $referral2 = str_replace("www.", "", $referral1); $referral3 = "http://".trim($referral2); $referral4 = "http://www.".$referral2; // If referrer is in database, then add a hit! if (!empty($referral1)){ $add_hit = mysql_query("update links set totalin=totalin+1, dayin=dayin+1 where url like '$referral1%' || url like '$referral3%' || url like '$referral4%' LIMIT 1"); // Update the user & domain stats if it was a user domain who the hit came from credit_user_domain($referral1,$referral2); // End user and domain update } ?> PHP: How can I get $_SERVER['HTTP_REFERER'] to work in the include file?
include ("config.php"); include ("functions.php"); should probably be include ("full/path/to/your/script/config.php"); include ("/and/full/path/to/your/script/functions.php");
Man, why spend much time when it can be achieved in a simple way? If the file does not want to see the referrer, add a line to get the referrer before you include the file! $referrer = $_SERVER['HTTP_REFERER']; include ("http://www.mysite.com/link-dump/plugs.php"); And in the plugs file, just treat $referrer with no need to use $_SERVER again!
could just be include('../link-dump/plugs.php'); some servers dont allow including codebases on http:// urls.
I agrre with Lordo and explain bit more $referral=$_SERVER['HTTP_REFERER']; //In main index file include ("http://www.mysite.com/link-dump/plugs.php"); In plugs.php <?php include ("config.php"); include ("functions.php"); //$referral = $_SERVER['HTTP_REFERER']; $array=parse_url($referral); $referral1 = $array['host']; $referral2 = str_replace("www.", "", $referral1); $referral3 = "http://".trim($referral2); $referral4 = "http://www.".$referral2; // If referrer is in database, then add a hit! if (!empty($referral1)) { $add_hit = mysql_query("update links set totalin=totalin+1, dayin=dayin+1 where url like '$referral1%' || url like '$referral3%' || url like '$referral4%' LIMIT 1"); // Update the user & domain stats if it was a user domain who the hit came from credit_user_domain($referral1,$referral2); // End user and domain update } ?>
Yeah I tried something similar to this. The problem is very much the same. When I do this: $referral=$_SERVER['HTTP_REFERER']; include ("http://www.mysite.com/link-dump/plugs.php"); $referral is considered nil once I enter my include file. As soon as I leave the include file, $referral is its correct value again. I appreciate the help so far. Any other suggestions?
a server side 'include' works on your server directory structure, it doesnt use the http protocol. The include function itself doesnt generate an error message if the library isn't found. So unless you set 'warnings' on, you wont know if the include fails. using an echo statement gives you some feedback. if you add a line to plugs.php echo 'okay'; then you know if the include-function loads the file the http-include wont work so then you can try include('../link-dump/plugs.php'); and that should produce that echo
Ok, I left it as include ("http://www.mysite.com/link-dump/plugs.php"); and put an echo in "plugs.php". The echo displays so the include is functioning correctly.
interesting i recreated it on my own server using a http:// include fails on my server because of an openbasedir restriction and a ../ reference works fine apart from that solution I wouldnt know any.
This is the base ideas you can use <?php //plugs.php include ("config.php"); include ("functions.php"); class REFERER{ public function check_referer($referer){ // referer code here } }//class end //you can include and use your referer class in any page your website like this //let say you call from index.php //index.php include('plugs.php'); $referer = new REFERER; $referer->check_referer($_SERVER['HTTP_REFERER']); //continue your code for this page //done ?> PHP:
there must be some error in plugin.php Try to include other file with only line echo $refferal in plugin.php and see if it prints or not Regards Alex
Thanks for all the help guys, I got it working now - phplover, I used a modified version of your suggestion to get it working thanks buddy. I left green rep to those suggesting code fixes.