Script that automatically redirects to a choice of links?

420bud

Newbie
Joined
Jul 25, 2011
Messages
1
Reaction score
0
Hi fellas, I just wanted to know if anyone knew how to make something that I could put my links in and upload it to my site, then it would automatically redirect them to a random link of my choice?

Basically the visitor wouldn't even see the page, just the link they type hat just redirects to one of the random pages straight away.

I'm dreadful at this type of thing so any help would be appreciated. :ranger:
 
Save this as a php file.

PHP:
<?php

$links[] = 'http://www.mysite1.com';
$links[] = 'http://www.mysite2.com'; 
$links[] = 'http://www.mysite3.com';

$choice = rand(0, count($links) -1);
header('Location: '.$links[$choice]);
?>

I just typed it - didn't check it. If there 's a problem, let me know.
 
PHP:
<?php
$links = array("http://link1.com", "http://link2.com", "http://link3.com");
header("location:".$links[array_rand($links)]);
?>
 
Save this as a php file.

PHP:
<?php

$links[] = 'http://www.mysite1.com';
$links[] = 'http://www.mysite2.com'; 
$links[] = 'http://www.mysite3.com';

$choice = rand(0, count($links) -1);
header('Location: '.$links[$choice]);
?>

I just typed it - didn't check it. If there 's a problem, let me know.
Beat me to it :p

I used to have issues using $links[] = ""; on some servers, it is 'best practice' I have been told to declare the array before using [] E.g. $links = array(); and then use $links[] = "";

array_rand also shortens the code a lot more ;)
 
I used to have issues using $links[] = ""; on some servers, it is 'best practice' I have been told to declare the array before using [] E.g. $links = array(); and then use $links[] = "";

True! That will happen if the vhost is configured to display php notices. PHP will issue a notice if the array is not initialized before adding a member. And the notice will break the header redirect, as there must be absolutely no textual output before the header command.

Thanks for pointing it out! :)

array_rand also shortens the code a lot more ;)

;)
 
Back
Top