PHP - Redirect to Random URLs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

PHP - Redirect to Random URLs

Post 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
User avatar
nor0101
Forum Commoner
Posts: 53
Joined: Thu Jan 15, 2009 12:06 pm
Location: Wisconsin

Re: PHP - Redirect to Random URLs

Post by nor0101 »

Responding to the only question in your post, referrer spoofing is indeed possible.

http://en.wikipedia.org/wiki/Referrer_spoofing
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

Re: PHP - Redirect to Random URLs

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP - Redirect to Random URLs

Post 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?
abcdabcd
Forum Newbie
Posts: 9
Joined: Tue Jan 27, 2009 11:28 pm

Re: PHP - Redirect to Random URLs

Post by abcdabcd »

the redirect problems have been fixed, just not referer
Post Reply