oneg Posted May 13, 2008 Share Posted May 13, 2008 I have this code for referer redirect: <? $referrer = $_SERVER['HTTP_REFERER']; if (preg_match("/outsidesite.com/",$referrer)) { header('Location: http://www.mysite1.com'); } else { header('Location: http://www.mysite.com'); }; ?> But the question is: If I want to match a LIST OF SITES stored on a txt file, insted of "outsidesite.com". How can I work with a txt list of sites: "mysites.txt" instead of only one site: "outsidesite.com" <? $referrer = $_SERVER['HTTP_REFERER']; if (preg_match("HERE A TXT WITH A LIST OF SITES mysites.txt",$referrer)) { header('Location: http://www.mysite1.com'); } else { header('Location: ht*tp://www.mysite.com'); }; ?> Anyone knows what to put here: ("HERE A TXT WITH A LIST OF SITES mysites.txt"), to make it functional. I am working with this: $file=file_get_contents("file.txt"); if (preg_match ("/$file/", ........... But it only retrives the first line in the txt file, not the second line. thank you. Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/ Share on other sites More sharing options...
MadTechie Posted May 13, 2008 Share Posted May 13, 2008 i think this is what your looking for.. but just so you know 'HTTP_REFERER' The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. <?php /* File example http://site1.com,http://site2.com,http://site3.com */ $file=file_get_contents("file.txt"); $sites = explode(",",$file); // change "," to "\n" for line delimitors $referrer = $_SERVER['HTTP_REFERER']; foreach($sites as $site) { if (preg_match("%$site%i",$referrer)) { // found header('Location: http://www.mysite1.com'); exit; } } //Not found header('Location: http://www.mysite.com'); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/#findComment-539806 Share on other sites More sharing options...
oneg Posted May 13, 2008 Author Share Posted May 13, 2008 Thank you for your answer. I am testing this (I added some missing "}"): <?php $file=file_get_contents("file.txt"); $sites = explode("\n",$file); // change "," to "\n" for line delimitors $referrer = $_SERVER['HTTP_REFERER']; foreach($sites as $site) { if (preg_match("%$site%i",$referrer)) { echo "works"; exit; } else { //Not found echo "dont work"; exit;} } ?> And my file.txt is: site1.com site2.com site3.com but only works for site1.com not for site2.com or site3.com. I dont know what I am doing wrong. Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/#findComment-539829 Share on other sites More sharing options...
BlueSkyIS Posted May 13, 2008 Share Posted May 13, 2008 file_get_contents returns the entire contents of the file in a single string. you probably want to use file() which returns an array with each line of the file as an element. Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/#findComment-539932 Share on other sites More sharing options...
oneg Posted May 13, 2008 Author Share Posted May 13, 2008 file_get_contents returns the entire contents of the file in a single string. you probably want to use file() which returns an array with each line of the file as an element. Thank you. I testing this code: <?php $file=file ("file.txt"); $sites = explode("\n",$file); // change "," to "\n" for line delimitors $referrer = $_SERVER['HTTP_REFERER']; foreach($sites as $site) { if (preg_match("%$site%i",$referrer)) { echo "works"; exit; } else { //Not found echo "dont work"; exit;} } ?> But the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/#findComment-539975 Share on other sites More sharing options...
MadTechie Posted May 13, 2008 Share Posted May 13, 2008 Their no was missing }, in my code use the /n should be fine for line breaks, you could try, the following (depends how the file was built) /r/n /n /r Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/#findComment-540102 Share on other sites More sharing options...
oneg Posted May 13, 2008 Author Share Posted May 13, 2008 Their no was missing }, in my code use the /n should be fine for line breaks, you could try, the following (depends how the file was built) /r/n /n /r Thank you very much for your time and help. Its working great now. Quote Link to comment https://forums.phpfreaks.com/topic/105406-solved-referere-redirect-txt/#findComment-540130 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.