JonEMD Posted December 20, 2011 Share Posted December 20, 2011 Hello People, I've been racking my brains, searching google and trawling through the many pages here to see if someone else has already asked the same question but I cant find it. I'm trying to build a simple redirect script based on the referring URL. I want to direct the user to "page a" if any of the arrays are found and "page b" if not. This is what I have so far <?php $main_string="@$HTTP_REFERER"; $arr = array('aaaa','bbbb','cccc'); foreach($arr as $key => $search_needle) { if(stristr($main_string, $search_needle) != FALSE) { echo "<meta http-equiv=Refresh content=0;url=http://www.google.com/PAGEA.html>"; } } ?> I thought that it might be a case of adding the "else" command but doing that will always redirect to "page b" if all of the arrays aren't true and only 1 needs to be. Anyone got any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/253573-referring-url-redirect/ Share on other sites More sharing options...
requinix Posted December 20, 2011 Share Posted December 20, 2011 Hint: you can't possibly know whether you should redirect unless you've looked at everything in the array first. You can't make a decision in the middle because, for all you know, the next one might match. Quote Link to comment https://forums.phpfreaks.com/topic/253573-referring-url-redirect/#findComment-1299932 Share on other sites More sharing options...
JonEMD Posted December 20, 2011 Author Share Posted December 20, 2011 That makes sense but I'm not sure what to do with it..... Currently, if any of the arrays match it goes to "page a" which is fine. The issue I have is if none of the arrays match nothing happens but I want it to redirect to "page b". How can i code that? What function would I need to use? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/253573-referring-url-redirect/#findComment-1299936 Share on other sites More sharing options...
requinix Posted December 21, 2011 Share Posted December 21, 2011 If you have code inside the loop that'll redirect if there's a match, right? So if you've left the loop nothing must have matched, right? Be sure you exit; after each call to header. Which, by the way, you should be using to redirect instead of a META tag. Quote Link to comment https://forums.phpfreaks.com/topic/253573-referring-url-redirect/#findComment-1299942 Share on other sites More sharing options...
m3bik Posted December 21, 2011 Share Posted December 21, 2011 Not sure if this helps, but I'd run something like this: $arr = array('aaaa','bbbb','cccc'); foreach($arr as $search_needle) They $key isn't required since there's no key in the array, just values.. Quote Link to comment https://forums.phpfreaks.com/topic/253573-referring-url-redirect/#findComment-1299945 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.