Home » Php » Randomly redirect with PHP

Randomly redirect with PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to redirect 2 URL randomly but it not working.

My Code

<?php
$urls=readStack();
if(empty($urls)) {
    $urls = shuffle(array('https://domain1.com', 'https://domain2.com')); 
    $url=array_pop($urls);
    storeStack($urls);
    header('Location:' .$url); 
?>

Help how to fix this?

How to&Answers:

I don’t know what readStack() and storeStack() are but you probably want something like

$arr = array('https://domain1.com', 'https://domain2.com');
shuffle($arr); // This returns a boolean and the array is modified inside the function so just pass in the array
$url=array_pop($arr); // Pop the top most element from the array and store into $url
header("Location: $url");

That code alone gives you a random url from those two