Php 从index.html随机重定向到另一个文件
每次用户来到我的主页,即Php 从index.html随机重定向到另一个文件,php,javascript,web,Php,Javascript,Web,每次用户来到我的主页,即索引文件,我希望脚本运行,以便每次查看我网站的不同且随机的页面 我更喜欢用Javascript或PHP来实现这一点。我想象的索引文件的伪代码如下所示: var randomNumber = functionThatReturnsRandomNumber(10); var urlRedirect; if (randomNumber == 0) urlRedirect = 'xxxx.com/folder0/index.html if (randomNumber
索引
文件,我希望脚本运行,以便每次查看我网站的不同且随机的页面
我更喜欢用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.
有什么想法吗
编辑
我想我需要更明确一些。有人能建议我如何完成上述任务吗?谢谢。使用重定向标题
<?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);
?>
使用重定向标题
<?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);
?>
+1提供卓越的用户体验 作为用户,最好在PHP级别上这样做,否则会不可避免地出现
加载->页面浏览->加载->新页面
(作为访问者,如果发生这种情况,我会感到不安)
但是,只要您心中有一个“可能的目的地”列表,就可以在index.php
的顶部使用如下内容:
<?php
$possibilities = array(/*...*/);
header('Location: ' + $possibilities[rand(0, count($possibilities) - 1)]);
+1提供卓越的用户体验
作为用户,最好在PHP级别上这样做,否则会不可避免地出现加载->页面浏览->加载->新页面
(作为访问者,如果发生这种情况,我会感到不安)
但是,只要您心中有一个“可能的目的地”列表,就可以在index.php
的顶部使用如下内容:
<?php
$possibilities = array(/*...*/);
header('Location: ' + $possibilities[rand(0, count($possibilities) - 1)]);
如果要使用Javascript,请使用var randomnumber=Math.floor(Math.random()*11)
生成介于1和10之间的随机数。然后使用window.location.href=urlRedirect
将用户重定向到所选页面。如果要使用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:
<?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");
?>
重定向到随机网站:
<?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");
?>
重定向到随机子目录:
<?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");
?>
重定向到随机网站:
<?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");
?>
你真正的问题是“有什么想法吗?”使用URL数组,这样你就不会浪费世界上提供的if()
语句了。。。它们是一个有限的资源,你知道…@jerome.s请参见编辑。“有想法吗?”是你真正的问题吗?使用URL数组,这样你就不会浪费世界上提供的if()
语句了。。。他们是一个有限的资源,你知道…@jerome.s请查看编辑。你需要rand(0,count($probabilities)-1)
;函数需要一个闭合的间隔,而不是半开放的。@PleaseStand:很好的调用,谢谢。这主要是由于使用rand(01000)%count()
而不是rand(0,count())
来获得更好的位移;p您需要兰特(0,计数($可能性)-1)
;函数需要一个闭合的间隔,而不是半开放的。@PleaseStand:很好的调用,谢谢。这主要是由于使用rand(01000)%count()
而不是rand(0,count())
来获得更好的位移;在我看来,这是最优雅的回答。为什么没有投票?在我看来,这是最优雅的答案。为什么没有选票?