Page 1 of 1

PHP - Redirect to Random URLs

Posted: Wed Jan 28, 2009 7:09 pm
by abcdabcd
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:

Code: Select all

<?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

Re: PHP - Redirect to Random URLs

Posted: Wed Jan 28, 2009 7:31 pm
by nor0101
Responding to the only question in your post, referrer spoofing is indeed possible.

http://en.wikipedia.org/wiki/Referrer_spoofing

Re: PHP - Redirect to Random URLs

Posted: Wed Jan 28, 2009 7:46 pm
by abcdabcd
nor0101 wrote:Responding to the only question in your post, referrer spoofing is indeed possible.

http://en.wikipedia.org/wiki/Referrer_spoofing
thanks for your reply, yes I understand it is possible but, I think people are saying it can't be done with php headers as in the above script?

Re: PHP - Redirect to Random URLs

Posted: Wed Jan 28, 2009 8:25 pm
by requinix
Two problems with that code:

First,

Code: Select all

$random = rand(0, 3);
$aff_array = array("http://www.RANDOMSITE1.COM",
                  "http://RANDOMSITE2.COM",
                  "http://RANDOMSITE3.COM");
rand() is inclusive so in the above code $random could be 0, 1, 2, or 3. However the highest index in $aff_array is 2. 25% of the time the redirect won't work. One way to fix it is

Code: Select all

$aff_array = array("http://www.RANDOMSITE1.COM",
                  "http://RANDOMSITE2.COM",
                  "http://RANDOMSITE3.COM");
$random = rand(0, count($aff_array) - 1);
Second,

Code: Select all

header('Location: $aff_array[$random]');
You're using single quotes. Variables don't work in single-quoted strings.

Code: Select all

header("Location: $aff_array[$random]");
header('Location: ' . $aff_array[$random]);
Pick one and go with it.

Oh, and while I'm at it:
abcdabcd wrote:The person that posted this script (they removed it, I think it had too many errors)
So you know it has "too many errors" but you're still using it?

Re: PHP - Redirect to Random URLs

Posted: Wed Jan 28, 2009 8:29 pm
by abcdabcd
the redirect problems have been fixed, just not referer