0

I have echoed out the header and the syntax appears correct but the redirect does not work.

The page determines the code from the URL.

The if statement is working correctly to determine which page is required, but the header is not redirecting the user to the URL specified.

Can anyone see what I have done wrong?



<!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>

Thanks

Alex Leo
  • 2,781
  • 2
  • 13
  • 29
  • Why would "not working" be a better problem description than "%24url appears in the redirected url" ? – mario Oct 24 '20 at 11:29
  • I am not sure what you mean. I am not getting %24 in the url. If I print or echo the URL or even the header code with ``` 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 ; echo "header('Location: $url')" ; header("Location: $url") ; die() ; } ``` I just see header('Location: https://example.com/dogs') on the page. So I can see that the syntax is correct, it just does not redirect. – Bmc Process Oct 24 '20 at 12:10
  • More duplicates added. – mario Oct 24 '20 at 12:23

1 Answers1

0

Use " " quotes when you are using variable inside string.So replace header('Location: $url') ; with header("Location: $url") ;

Gnanavel
  • 740
  • 6
  • 18
  • thanks .. I had tried that. I have updated my code. I am still getting the same response. The page does not redirect to the header. Instead I stay on the page with a blank screen. Is the die killing it off too soon? – Bmc Process Oct 24 '20 at 11:52
  • Yes dont' use `die` or `exit` after `header`.Read this https://stackoverflow.com/questions/8665985/php-utilizing-exit-or-die-after-headerlocation – Gnanavel Oct 24 '20 at 12:33
  • Thank you. This solved the issue. Putting the header within the die(); has solved the issue. die( header( "Location: $url" ) ); – Bmc Process Oct 24 '20 at 13:35