Jump to content


Photo

[resolved] Random Redirect from Array - How?


  • Please log in to reply
3 replies to this topic

#1 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 30 October 2006 - 11:45 PM

I'm in need of a php script to redirect from an array of URL's....

For example when index.php is hit, it will redirect to a random url from the array....

<?php
$url = array(
"1" => "http://www.google.com",
"2" => "http://www.yahoo.com",
"3" => "http://www.snap.com",
"4" => "http://www.altavista.com",
);
?>

<HTML><HEAD>
<META HTTP-EQUIV=Refresh CONTENT="0; URL=<? echo("$newurl"); ?>">
</HTML>



That's as far as I can go, I don't know how to tell it to pick randomly....:-(

Sorry, I'm learning.......

#2 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 31 October 2006 - 12:06 AM

<?php
$urlIdx = rand(0,count($url)-1);
?>

<META HTTP-EQUIV=Refresh CONTENT="0; URL=<? echo("$url[$urlIdx]"); ?>">

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 31 October 2006 - 12:11 AM

Or, you just do something like this:
<?php

$url = array(
"http://www.google.com",
"http://www.yahoo.com",
"http://www.snap.com",
"http://www.altavista.com"
);

shuffle($url);
header("Location: $url[0]"); // Redirect user...

?>


#4 holyearth

holyearth
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 31 October 2006 - 12:20 AM

<?php
$url = array(
"0" => "http://www.excite.com",
"1" => "http://www.google.com",
"2" => "http://www.yahoo.com",
"3" => "http://www.snap.com",
"4" => "http://www.altavista.com",
);
?>

<?php
$urlIdx = rand(0,count($url)-1);
?>


<META HTTP-EQUIV=Refresh CONTENT="0; URL=<? echo("$url[$urlIdx]"); ?>">


<!--THE ABOVE CODE WORKS!!-->




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users