[英]Redirecting randomly from index.html to another file
每次用户访问我的主页(即index
文件)时,我都希望运行脚本,以便每次查看我的网站的不同且随机的页面。
我更愿意用Javascript或PHP做到这一点。 我想象的索引文件的伪代码看起来像这样:
var randomNumber = functionThatReturnsRandomNumber(10);
var urlRedirect;
if (randomNumber == 0)
urlRedirect = 'xxxx.com/folder0/index.html
if (randomNumber == 1)
urlRedirect = 'xxxx.com/folder1/index.html
if (randomNumber == 2)
urlRedirect = 'xxxx.com/folder2/index.html
...
if (randomNumber == 9)
urlRedirect = 'xxxx.com/folder9/index.html
然后是一些将浏览器重定向到urlRedirect.
代码urlRedirect.
有什么想法吗?
编辑
我想我需要更加明确。 有人可以建议我如何实现上述目标吗? 谢谢。
+1可提供出色的用户体验。
作为用户,您最好在PHP级别上这样做,否则不可避免地会出现加载loading->page glimpse->loading->new page
((作为访问者,如果发生这种情况,我会感到很粗略)。
但是,只要您牢记“可能的目的地”列表,就可以在index.php
顶部使用以下内容:
<?php
$possibilities = array(/*...*/);
header('Location: ' + $possibilities[rand(0, count($possibilities) - 1)]);
尽管我可能将其与会话或Cookie结合使用,所以它仅在首次访问时有效(除非您希望每次都可以使用)。
使用重定向标头。
<?php
$location = "http://google.com";
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
?>
对于随机重定向:
<?php
$urls = array('http://1.com',"http://2.com","http://3.com"); //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);
?>
如果要使用Javascript,请使用var randomnumber=Math.floor(Math.random()*11);
生成1到10之间的随机数。然后使用window.location.href=urlRedirect;
将用户重定向到您选择的页面。
使用PHP:
<?php
$randomNumber = rand(10);
$urlRedirect = '';
if ($randomNumber == 0)
$urlRedirect = 'xxxx.com/folder0/index.html';
if ($randomNumber == 1)
$urlRedirect = 'xxxx.com/folder1/index.html';
if ($randomNumber == 2)
$urlRedirect = 'xxxx.com/folder2/index.html';
...
if ($randomNumber == 9)
$urlRedirect = 'xxxx.com/folder9/index.html';
header ('Location: '.$urlRedirect);
重定向到随机子目录:
<?php
$myLinks = array("dir-1/",
"dir-2/",
"dir-3/",
"dir-4/",
"dir-5/");
$randomRedirection = $myLinks[array_rand($myLinks)];
header("Location: $randomRedirection");
?>
重定向到随机网站:
<?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");
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.