简体   繁体   中英

PHP Redirect - Won't Stay on Current Page

<?php

  $links = array();

  $links[] = ''; //i left this blank so it stays on the current page, problem causer? 
  $links[] = 'http://domain2.com';
  $links[] = 'http://domain3.com';
  $links[] = 'http://domain4.com';

  $link = $links[array_rand($links)];

  header("Location: {$link}");

?>

What I want it to do is either redirect to domain 2, 3, 4 OR stay on the same page its on currently. I want it to be completely random, like the above script.

The above code works on Chrome, however, on FireFox and Internet Explorer it says it is

server is redirecting the request for this address in a way that will never complete

Is there any way to make it work on ALL browsers?

<?php

$links = array();

$links[] = ''; 
$links[] = 'http://domain2.com';
$links[] = 'http://domain3.com';
$links[] = 'http://domain4.com';

$link   = $links[array_rand($links)];

if ($link !== '') { // Don't pass an empty Location
    header("Location: $link"); // Remove the {}
}

?>

Remove the {} and it should be alright. Also, I think it would be best to not pass an empty Location header.

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