如何在用户进入主页面时显示用户随机子网页(index.php)?

时间:2022-07-29 21:15:13

I have a domain registered, e.g. mydomain.com . I would like to put there some php page (or whatever - html with jquery?, etc?) that would have a small script - when user enters this page, he will be immediately randomly redirected to one of my subpages (that are also on the same level as index.php, for example first.php, second.php, third.php) - is that achievable?

我有一个域名注册,例如mydomain.com。我想在那里放一些php页面(或者其他什么 - 带jquery的html?等等?)会有一个小脚本 - 当用户进入这个页面时,他会立即被随机重定向到我的一个子页面(也是与index.php相同的级别,例如first.php,second.php,third.php) - 可以实现吗?

2 个解决方案

#1


How about a javascript solution:

怎么样的JavaScript解决方案:

  var webpages = ['url1', 'url2', 'url3'];
  //you can have an array of the urls

  var randomIndex = Math.floor(Math.random() * webpages.length - 1);
  //then generate a valid random index

  window.location.href = webpages[randomIndex];
  //redirect to a subpage randomly

#2


This would scan a directory using php, pick a random file in the directory, and redirect to it. You'd need to make sure everything in the folder was ok to redirect to.

这将使用php扫描目录,在目录中选择一个随机文件,然后重定向到它。您需要确保文件夹中的所有内容都可以重定向到。

$dir    = '/subpages';
$pages = scandir($dir);
$randomPage=$pages[rand(0,sizeof($pages)];
header('Location: http://example.com/'.$randomPage);

#1


How about a javascript solution:

怎么样的JavaScript解决方案:

  var webpages = ['url1', 'url2', 'url3'];
  //you can have an array of the urls

  var randomIndex = Math.floor(Math.random() * webpages.length - 1);
  //then generate a valid random index

  window.location.href = webpages[randomIndex];
  //redirect to a subpage randomly

#2


This would scan a directory using php, pick a random file in the directory, and redirect to it. You'd need to make sure everything in the folder was ok to redirect to.

这将使用php扫描目录,在目录中选择一个随机文件,然后重定向到它。您需要确保文件夹中的所有内容都可以重定向到。

$dir    = '/subpages';
$pages = scandir($dir);
$randomPage=$pages[rand(0,sizeof($pages)];
header('Location: http://example.com/'.$randomPage);