Php de enlace aleatorio

I am trying to setup this code that loads a random link but I have one problem:

$links = file('myfile.txt');
$rand_link = $links[ mt_rand(0, count($links) - 1) ];
echo '<div class="link"><a href="' . $rand_link . '"><img src="button.png" border="0"></a></div>';

It sometimes shows the pages it has shown earlier. I want it to remember what sites it has displayed and don’t show it again until the user starts from the beginning. Thanks appreciate your help.

preguntado el 09 de enero de 11 a las 08:01

I couldn't embed the code there but it looks like this. $links = file('myfile.txt'); $rand_link = $links[ mt_rand(0, count($links) - 1) ]; echo '<div class="link"><a href="' . $rand_link . '"><img src="button.png" border="0"></a></div>'; -

2 Respuestas

finally the enhanced version with multiple sites in history

// Predefinitions
$links = file('myfile.txt');
$currentPage = $_SERVER['PHP_SELF'];

// make sure links are pure and no whitespaces are left
foreach ( $links as &$link )
    $link = trim($link);

// prepare for history
if ( ! array_key_exists('visited_links', $_SESSION) )
    $_SESSION['visited_links'] = array();
// add current site to visited links if it wasnt visited earlier
if ( ! in_array($currentPage, $_SESSION['visited_links']) )
    $_SESSION['visited_links'][] = $currentPage; 

// get all those links which weren't visited yet kinda $links = $allLinks - $visitedLinks
$potentialLinks = array_diff($links, $_SESSION['visited_links']);

$randomLinkId = mt_rand(0, count($potentialLinks)-1);
$randomLinkIds = array_keys($potentialLinks);
$randomLink = $potentialLinks[$randomLinkIds[$randomLinkId]];

echo '<div class="link"><a href="'.$randomLink.'"><img src="button.png" border="0"></a></div>';

there could be potential errors if the format of $_SERVER['PHP_SELF'] and this of your linklist differ, you can evaluate this with some quick var_dumps. As far as I know Wordpress automatically starts an session, this should explain your errormessage. This script assumes that the session is already started.

Respondido el 10 de enero de 11 a las 03:01

unfortunately i am very much beginner. can you help implement this? - Truthworthy

$arrayOFRandomLinks should hold all those links you want to iterate through, you can also do $arrayOFRandomLinks = include 'somefile.php' with somefile.php simply being <?php return array('link1.php', 'link2.php', ...); I added the code to make the rand work, that should be the whole magic - Samuel Herzog

i just understood that you don't want to show a set of sites, not a single one, for this approach you need to use sessions, i'll implement this too, just give me some time. - Samuel Herzog

hI Sameul apprecate your time, this seems to be the closest i just faced little error here it says .... Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in... yes the links are external. - Truthworthy

could you give me an excerpt of your myfile.txt so i can test it myself? - Samuel Herzog

Try this code. May be errors, I have not tested on the server. And I use the session to preserve already viewed pages.

$links = file('myfile.txt');
$show_links = array();


if( $_SESSION['show_links'] )
foreach($_SESSION['show_links'] as $key=>$value){
    $show_links[$key] = $value;

$random_link_number = mt_rand(0, count($links) - 1);

if(count($show_links) != count($links)){
while(   !in_array($random_link_number, $show_links)   ){
   $random_link_number = mt_rand(0, count($links) - 1);

$show_links[] = $random_link_number;
$_SESSION['show_links'] = $show_links;

$rand_link = $links[ $random_link_number ];

Respondido el 09 de enero de 11 a las 12:01

thanks a lot for answer...unfortunately it shows errors like this Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent Fatal error: Maximum execution time of 30 seconds exceeded is there another way we can accomplish this or write this code? thanks appreciate your help. - Truthworthy

nope still experiencing same issue. removed the session_start (); since i am adding this code to a wordpress plugin but still i have the other error. - Truthworthy

But how then you ought to save the data that looked at this site? - Жасулан Бердибеков

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.