简体   繁体   中英

How can I show the user random subwebpage when he enters my main page (index.php)?

I have a domain registered, eg 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?

How about a javascript solution:

  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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM