PHP标头重定向不适用于Variable

时间:2020-10-24 11:25:25

标签: php html

我已经回显了标头,语法似乎正确,但是重定向不起作用。

该页面从URL确定代码。

if语句可以正常工作以确定所需的页面,但标头未将用户重定向到指定的URL。

有人可以看到我做错了吗?



<!DOCTYPE html>
<html>
<head>
<title>Finding your code ... </title>
</head>
<body>

<?php
// directs the user to page based on code

$code = $_GET['c'] ;
$code = (int) $code;
$page1 = 'https://example.com/dogs';
$page2 = 'https://example.com/cats';
$page3 = 'https://example.com/frogs';
$page4 = 'https://example.com/cows';
$page5 = 'https://example.com/else';




if ( $code >= 1 && $code <= 10  || $code >= 100 && $code <= 150 ){
   // Do something when num is between 1 and, or 100 and 150

//echo $page1 ; 
$url = $page1 ; 
header("Location: $url") ;
  die() ;
}

if($code >= 11 && $code <= 20) {
$url = $page2 ; 
echo $url ; 
header("Location: $url") ;
    die() ;
}

if($code >= 21 && $code <= 30) {
$url = $page3 ; 
header("Location: $url") ;
    die() ;
}

if($code >= 31 && $code <= 40) {
$url = $page4 ; 
header("Location: $url") ;
    die() ;
}


else {
$url = $page5 ; 
header("Location: $url") ;
    die() ;
}




?>

</body>
</html>

谢谢

1 个答案:

答案 0 :(得分:0)

在字符串中使用变量时使用引号。因此,将header('Location: $url') ;替换为header("Location: $url") ;