1

I am trying to make a page pick a link and then go to link there are 2 links that I am trying for it to decide from I have read PHP Manual and used whats in there to make this

$input = array("https://order.abcgameservers.com/aff.php?aff=47", 
"https://discord.gg/CjzZRBq");
$answer = $rand_keys = array_rand($input, 1);
echo "header('Location: $answer')";

it echoes header('Location: 1')

Jonathan Connery
  • 23
  • 1
  • 1
  • 5

2 Answers2

2

There's three things to note about this.

  1. header() should not be echo'ed, it returns void.
  2. array_rand() will just return the key of an element, not the value.
  3. You should always use exit; after header("Location: .."); (although it might not be relevant if its the end of the script).

From the manual (emphasis mine)

Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.

After implementing those changed, your code would look like this

$input = array("https://order.abcgameservers.com/aff.php?aff=47", "https://discord.gg/CjzZRBq");
$answer = $rand_keys = array_rand($input);
header('Location: '.$input[$answer]);
exit;

You'll notice that the header() has been altered in two ways: The echo has been removed (and the quotes that went along with that), and that it targets $input[$answer] instead of $answer.

Alternatively, you can use array_flip() to flip the value/index pairs. If you use array_rand() on that, you'd get a random value!

$answer = array_rand(array_flip($input));
header('Location: '.$answer);
exit;
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

As explained in the PHP.net website, array_rand() outputs a random key in an array set and not the value it self. So in your case, you need to use the provided key to retrieve the corresponding value. $input[$answer].

Also, the redirect should not be in an echo. header() is a function and can't work the way you used it in your example. Although, I am pretty sure you did that just to debug your script lol

Try this:

$input = array("https://order.abcgameservers.com/aff.php?aff=47", "https://discord.gg/CjzZRBq");
$answer = array_rand($input, 1);
header('Location: '.$input[$answer]);
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38