0

I am trying to create a redirect system on my site that will redirect a user from one php page to another random pages.

$rand_link=rand(20,60);
if($rand_link > 20)
{$link="/page2.php";}
if($rand_link > 30)
{$link="/page3.php";}
else
{$link="/default.php";}
header('Location: $link');

Is not working, this code is on my "main.php" file, when I enter "/main.php" I am getting redirected to 404 page of my hosting service.

Is there something wrong with my code?

How can I minimise this code? I have more then 15 links to redirect randomly?

Any help would be greatly appriciated?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • The duplicate exactly shows you the difference between single and double quotes and that variables aren't parsed in single quotes. <- And that is exactly the problem in your code + the solution to use double quotes – Rizier123 Aug 02 '15 at 09:28
  • good this post also solve my problem. – Naeem Ul Wahhab Oct 21 '17 at 08:37

1 Answers1

6

Between single quotes ('), variables are not interpreted.

Use double quotes:

header("Location: $link");

or, use string concatenation:

header('Location:'.$link);

To randomize:

$rand=rand(1,15);
header('Location:/page'.$rand.'.php');
Pang
  • 9,564
  • 146
  • 81
  • 122
user2267379
  • 1,067
  • 2
  • 10
  • 20