Skip to content
Advertisement

Redirect error in php with isset and GET variable

I am trying to make some redirects to lock the page from having a different get variable from what i have defined. But the problem is that I am getting a redirect error which is The page isn’t redirecting properly.

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.

I tried different things but I could not solve this problem. I need your help please. It is to note that the variables w_news and the rest are coming from links on the page.

these are the following code which are in the header of the project:

  // Redirect function
  function redirect_to($redirect_link) {
    header("Location: ". $redirect_link);
    exit;
  }

  $redirect_link = "index.php?sec=w_news";
  //if sec is empty i want to redirect to the above link
  if (!isset($_GET['sec']) || isset($_GET['sec']) && $_GET['sec'] == "") {
    redirect_to($redirect_link);
  } else if (isset($_GET['sec']) && $_GET['sec'] != "w_news" || $_GET['sec'] != "pol" || $_GET['sec'] != "sci" || $_GET['sec'] != "tech" || $_GET['sec'] != "spo" || $_GET['sec'] != "covid19"){
    // and if the value is not = to the named ones i want also to redirect
    redirect_to($redirect_link);
  } 

Thank you in advance 🙂

Advertisement

Answer

<?php

if ($_GET['sec'] == "w_news" || $_GET['sec'] == "pol" || $_GET['sec'] == "sci" || $_GET['sec'] == "tech" || $_GET['sec'] == "spo" || $_GET['sec'] == "covid19")
    {
    // working  validate
    }
else
    {
    //failed redirect"; 
    $redirect_link = "index.php?sec=w_news";
    header("Location: ". $redirect_link);
    }   

?>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement