parka Posted February 26, 2007 Share Posted February 26, 2007 Hi all, I'm trying to print a referral link on my page, the code is as follows: $refererSite = $_SERVER['HTTP_REFERER']; if ($refererSite !== null){ print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n"); } When I put it onto a page, it'll get the Undefined Index error. It does so if a user were to have the page listed as homepage and was not directed there from other pages. What's the recommended way to getting around this? Should I suppress the error by adding "@"? Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/ Share on other sites More sharing options...
magnetica Posted February 26, 2007 Share Posted February 26, 2007 Are you trying this on a server you have set-up on your PC? Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194310 Share on other sites More sharing options...
parka Posted February 26, 2007 Author Share Posted February 26, 2007 Are you trying this on a server you have set-up on your PC? Yup. The server is on my PC. Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194311 Share on other sites More sharing options...
magnetica Posted February 26, 2007 Share Posted February 26, 2007 Yea I tried doing the same thing on my local server I think there is a setting in the php.ini that you have to change?! Try it on your hosting service. See if it works there Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194313 Share on other sites More sharing options...
Yesideez Posted February 26, 2007 Share Posted February 26, 2007 Try this... <?php $refererSite=$_SERVER['HTTP_REFERER']; if (!empty($refererSite)) { print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n"); } Note that if you enter the URL that script is stored on your PC directly and not linking to it from another page then the referer will be empty - I got caught out by that! Oh, and as magnetica says Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194314 Share on other sites More sharing options...
parka Posted February 26, 2007 Author Share Posted February 26, 2007 Try this... <?php $refererSite=$_SERVER['HTTP_REFERER']; if (!empty($refererSite)) { print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n"); } Note that if you enter the URL that script is stored on your PC directly and not linking to it from another page then the referer will be empty - I got caught out by that! Oh, and as magnetica says Thanks. If it's empty, I can have a "else" print nothing, right? Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194317 Share on other sites More sharing options...
Yesideez Posted February 26, 2007 Share Posted February 26, 2007 This will work: <?php $refererSite=$_SERVER['HTTP_REFERER']; if (!empty($refererSite)) { print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n"); } else {} But I don't know why you'd need an empty else. Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194322 Share on other sites More sharing options...
parka Posted February 26, 2007 Author Share Posted February 26, 2007 This will work: <?php $refererSite=$_SERVER['HTTP_REFERER']; if (!empty($refererSite)) { print("<a href='$refererSite'>Back to $refererSite</a><br /><br />\n"); } else {} But I don't know why you'd need an empty else. Good point. Just realised that. Anyway, I still have the undefined index caused by $_SERVER['HTTP_REFERER']. Should I just suppress it with "@". Is it good programming practice? Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194325 Share on other sites More sharing options...
Yesideez Posted February 26, 2007 Share Posted February 26, 2007 You can supress it but it's not really good programming practise. Errors should really be trapped but I never bother as I do my best to avoid errors being generated as I've not yet got around to figuring out trapping *oh the shame of me* Link to comment https://forums.phpfreaks.com/topic/40164-http_referer-undefined/#findComment-194327 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.