简体   繁体   中英

How to load random URL from a file in php

I am trying to create news portal. and I want to load random news link which is in the list.txt file and created a php script which calls the list.txt and load the link from the list.

checkout my codes.

script.php

    <?php 

$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);

// Here is our random link URL
$picked = $loadlist[$rand];
?>

<meta http-equiv="refresh" content="2;url=<?php echo $picked; ?>">

list.txt

http://news.com/news1.html
http://news.com/news2.html
http://news.com/news3.html
http://news.com/news4.html
http://news.com/news5.html

Above code is working fine now, thanks

Get a random integer based on item count of your $loadlist array.

$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);

// Here is our random link URL
$picked = $loadlist[$rand];

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