最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Redirecting randomly from index.html to another file - Stack Overflow

programmeradmin0浏览0评论

Every time a user es to my home page i.e. the index file I would like a script to run so a different and random page of my website is viewed each time.

I would prefer to do this in either Javascript or PHP. The pseudo code of the index file I would imagine would look something like this:

var randomNumber = functionThatReturnsRandomNumber(10);
var urlRedirect; 

if (randomNumber == 0)
    urlRedirect = 'xxxx/folder0/index.html

if (randomNumber == 1)
    urlRedirect = 'xxxx/folder1/index.html

if (randomNumber == 2)
    urlRedirect = 'xxxx/folder2/index.html

...

if (randomNumber == 9)
    urlRedirect = 'xxxx/folder9/index.html

and then some code that redirects the browser to urlRedirect.

Any thoughts?

EDIT

I guess I need to be more explicit. Would someone please suggest how I could acplish the above? Thanks.

Every time a user es to my home page i.e. the index file I would like a script to run so a different and random page of my website is viewed each time.

I would prefer to do this in either Javascript or PHP. The pseudo code of the index file I would imagine would look something like this:

var randomNumber = functionThatReturnsRandomNumber(10);
var urlRedirect; 

if (randomNumber == 0)
    urlRedirect = 'xxxx./folder0/index.html

if (randomNumber == 1)
    urlRedirect = 'xxxx./folder1/index.html

if (randomNumber == 2)
    urlRedirect = 'xxxx./folder2/index.html

...

if (randomNumber == 9)
    urlRedirect = 'xxxx./folder9/index.html

and then some code that redirects the browser to urlRedirect.

Any thoughts?

EDIT

I guess I need to be more explicit. Would someone please suggest how I could acplish the above? Thanks.

Share Improve this question edited Jan 11, 2013 at 15:44 Eric Brotto asked Jan 11, 2013 at 15:38 Eric BrottoEric Brotto 54.3k35 gold badges130 silver badges176 bronze badges 2
  • Is "Any thoughts ?" your actual question ? – wakooka Commented Jan 11, 2013 at 15:40
  • 3 Use an array for the urls, so you're not wasting the world's supply of if() statements... they're a finite resource, you know... – Marc B Commented Jan 11, 2013 at 15:42
Add a ment  | 

5 Answers 5

Reset to default 4

+1 for excellent user experience.

As a user, you're better off doing so on the PHP level, otherwise there's inevidably a hiccup of loading->page glimpse->loading->new page (and as a visitor, i would feel sketchy if that happened).

But, as long as you have a list of "possible destinations" in mind, you can use something like the following in the top of your index.php:

<?php
  $possibilities = array(/*...*/);
  header('Location: ' + $possibilities[rand(0, count($possibilities) - 1)]);

though I'd probably couple that with either a session or cookie so it only works on the first visit (unless you want it to work every time).

If you're going to use Javascript, use var randomnumber=Math.floor(Math.random()*11); to generate the random number between 1 and 10. And then use window.location.href=urlRedirect; to redirect the user to your chosen page.

Use a redirect header.

 <?php
 $location = "http://google.";
 header ('HTTP/1.1 301 Moved Permanently');
 header ('Location: '.$location);
 ?>

For random redirection:

<?php
$urls = array('http://1.',"http://2.","http://3."); //specify array of possible URLs
$rand = rand(0,count($urls)-1); //get random number between 0 and array length
$location = $urls[$rand]; //get random item from array
header ('HTTP/1.1 301 Moved Permanently'); //send header
header ('Location: '.$location);
?>

Use PHP:

<?php
$randomNumber = rand(10);
$urlRedirect = '';

if ($randomNumber == 0)
    $urlRedirect = 'xxxx./folder0/index.html';

if ($randomNumber == 1)
    $urlRedirect = 'xxxx./folder1/index.html';

if ($randomNumber == 2)
    $urlRedirect = 'xxxx./folder2/index.html';

...

if ($randomNumber == 9)
    $urlRedirect = 'xxxx./folder9/index.html';

header ('Location: '.$urlRedirect);

Redirection to a random sub directory:

<?php 
$myLinks = array("dir-1/", 
    "dir-2/",
    "dir-3/",
    "dir-4/",
    "dir-5/");

$randomRedirection = $myLinks[array_rand($myLinks)]; 
header("Location: $randomRedirection"); 
?>

Redirection to random web site:

<?php 
$myLinks = array("http://www.my-site.ie", 
    "http://www.my-site.eu",
    "http://www.my-site.de", 
    "http://www.my-site.it", 
    "http://www.my-site.uk");

$randomRedirection = $myLinks[array_rand($myLinks)]; 
header("Location: $randomRedirection"); 
?> 
发布评论

评论列表(0)

  1. 暂无评论