abcdabcd Posted January 29, 2009 Share Posted January 29, 2009 Hello, I'm trying to setup php code (i'm new to this) what I'm trying to do is list 3 different websites in the php code and have the website visitor sent to any of the listed websites randomly. Here's my current code which isn't working: <?php if($_SERVER['HTTP_REFERER']) { $random = rand(0, 3); $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); header('Location: $aff_array[$random]'); exit(); } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> If you're experienced in php you can probably understand what the code does but, I'll explain anyway. First there's a code that determines if the website visitor has a referer (ie: came from another website) if they have a referer I would like to them to be redirected randomly to one of the above websites. If they don't have a referer then the website just loads it's content (reason I left the html tags at the bottom of the code) and doesn't redirect. The person that posted this script (they removed it, I think it had too many errors) also said this: "What that does, it creates a random number from 0 to 3, then in the aff_array put all of your different websites, only 1 of them will be chosen to be redirected to." They also stated this: "Just to clear things up again, this script changes the referrer from wherever you posted your link to the referrer of the website with above code and then randomly picks 1 of several websites and redirects to it. " I checked yesterday on various php forums and was told that it's not possible to change the referer? Is that correct? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/ Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Share Posted January 29, 2009 What is this supposed to achieve? if($_SERVER['HTTP_REFERER']) { also try <?php $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".rand(0,2));//0,2 because array keys start at 0 exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749124 Share on other sites More sharing options...
genericnumber1 Posted January 29, 2009 Share Posted January 29, 2009 try.. <?php if($_SERVER['HTTP_REFERER']) { $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); header('Location: '.$aff_array[array_rand($aff_array)]); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749127 Share on other sites More sharing options...
ratcateme Posted January 29, 2009 Share Posted January 29, 2009 i am fairly sure you cannot change the refer if you are using header redirects. i think if you redirect with javascript or a meta tag (Google both you will find info on both) the redirect should change. also a easier way to get a random element from a array shuffle() like this: $aff_array = shuffle(array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM")); header('Location: ' . $aff_array[0]); and just looking at your code header('Location: $aff_array[$random]'); wont work but header("Location: $aff_array[$random]"); will Scott. Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749128 Share on other sites More sharing options...
ratcateme Posted January 29, 2009 Share Posted January 29, 2009 <?php $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".rand(0,2));//0,2 because array keys start at 0 exit(); ?> how exactly is that meant to work it would make a header looking like Location: 0 or Location: 1 should it look like <?php $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); ?> also i don't see the point in randomizing your selection twice if it is random then one randomization should yield the same spreed as 2 or 3 randomizations Scott. Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749129 Share on other sites More sharing options...
genericnumber1 Posted January 29, 2009 Share Posted January 29, 2009 Actually, shuffle() takes a reference and returns a bool. So you'd need to do this.. <?php $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); shuffle($aff_array); header('Location: ' . $aff_array[0]); It's annoying to remember which functions take references, especially when the function can possibly take such a small data set as shuffle() does in this case, so it's a completely understandable mistake. Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749130 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Share Posted January 29, 2009 Oops i missed out a bit should of been <?php $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749131 Share on other sites More sharing options...
abcdabcd Posted January 29, 2009 Author Share Posted January 29, 2009 try.. <?php if($_SERVER['HTTP_REFERER']) { $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); header('Location: '.$aff_array[array_rand($aff_array)]); exit(); } ?> Thanks for everybodys quick replies. This is the code I tried since it was the only one with if($_SERVER['HTTP_REFERER']). That code is used to forward visitors to one of those random websites if they have a website referer. Example: If they typed in the url directly into the browser they shouldn't have a referer so the current page will load and the user will not be redirected. So for the other codes I'll try I guess, I just place: <?php if($_SERVER['HTTP_REFERER']) { above where the $aff_array = array code starts? the error I got first was error in line 103 and I checked my script and line 103 there is nothing on there, it's a blank line right below the last </html> closing tag. I deleted the line and tried again then I got the same error when I trying myself: 403 forbidden. Here's what my code looks like now, is this correct: <?php if($_SERVER['HTTP_REFERER']) { $aff_array = array("http://www.RANDOMWEBSITE1.COM", "http://RANDOMWEBSITE2.COM", "http://RANDOMWEBSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749142 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Share Posted January 29, 2009 But that will mean if they visit your site and go to another one of your sites pages then back to this one they will be redirected randomly Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749143 Share on other sites More sharing options...
abcdabcd Posted January 29, 2009 Author Share Posted January 29, 2009 But that will mean if they visit your site and go to another one of your sites pages then back to this one they will be redirected randomly My website is only one page but, thanks for letting me know since that didn't occur to me. Edit: actually I realized this could be a problem if they visit any website and click back on their browser same thing could occur correct? I'll have to test to find out. Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749149 Share on other sites More sharing options...
abcdabcd Posted January 29, 2009 Author Share Posted January 29, 2009 Oops i missed out a bit should of been <?php $aff_array = array("http://www.RANDOMSITE1.COM", "http://RANDOMSITE2.COM", "http://RANDOMSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); ?> I tried this code as well and got - Parse error: syntax error, unexpected $end in xxxxxxx on line 102 this is what the code looks like: <?php if($_SERVER['HTTP_REFERER']) { $aff_array = array("http://www.RANDOMWEBSITE1.COM", "http://RANDOMWEBSITE2.COM", "http://RANDOMWEBSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> here's are the last few lines of index.php and line 102 is </html> </div><!-- end #footer --> </div> </body> </html> any help would be appreciated, thanks Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749153 Share on other sites More sharing options...
DeanWhitehouse Posted January 29, 2009 Share Posted January 29, 2009 <?php if($_SERVER['HTTP_REFERER']) { $aff_array = array("http://www.RANDOMWEBSITE1.COM", "http://RANDOMWEBSITE2.COM", "http://RANDOMWEBSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); ?> should be <?php if($_SERVER['HTTP_REFERER']) { $aff_array = array("http://www.RANDOMWEBSITE1.COM", "http://RANDOMWEBSITE2.COM", "http://RANDOMWEBSITE3.COM"); shuffle($aff_array); //to make it slightly more random header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0 exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749157 Share on other sites More sharing options...
abcdabcd Posted January 29, 2009 Author Share Posted January 29, 2009 Thanks! somebody from another forum also recommended code to use and didnt' work so I combined both your codes together to get a working code and it works! here it is: [code] <?php if($_SERVER['HTTP_REFERER'] != "") { $random = rand(0, 2); $aff_array = array("http://www.RANDOMWEBSITE1.COM", "http://RANDOMWEBSITE2.COM", "http://RANDOMWEBSITE3.COM"); header("Location: ".$aff_array[rand(0,2)]); exit(); } ?> Now the only problem is the referer but, there doesn't seem to be a way to change that with php headers. Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749171 Share on other sites More sharing options...
premiso Posted January 29, 2009 Share Posted January 29, 2009 mmmm, you could just use array_rand no? <?php if($_SERVER['HTTP_REFERER'] != "") { $aff_array = array("http://www.RANDOMWEBSITE1.COM", "http://RANDOMWEBSITE2.COM", "http://RANDOMWEBSITE3.COM"); header("Location: ".array_rand($aff_array)); exit(); } ?> If you want to send a false referrer, you would need to use cURL, however that is not really redirecting the user Quote Link to comment https://forums.phpfreaks.com/topic/142886-random-website-redirect/#findComment-749217 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.