-1

Am using the below code to redirect if the url string matches how ever the code is throwing "Warning: Use of undefined constant" error also the code does not work if the String is Case Sensitive, Please help me solve this issue (php version 7.2)

<?php 
if(isset($_GET['Keyword'])){
    if($_GET['Keyword'] == Test){
        header('Location: http://www.anotherURL.com');
        exit ();
    }
}
?>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Unless `Test` is a [constant](https://www.php.net/manual/en/language.constants.php), you need to put it in quotes: `$_GET['Keyword'] == 'Test'`, just like any other [string in PHP](https://www.php.net/manual/en/language.types.string.php) – M. Eriksson Oct 19 '22 at 16:17
  • Does this answer your question (regarding Case sensitive vs. Case insensitive)? [Case insensitive string comparison](https://stackoverflow.com/questions/5473542/case-insensitive-string-comparison) – Uwe Oct 19 '22 at 16:26

1 Answers1

0

actually your error says

Warning: Use of undefined constant

means Test used on line 3 in code mentioned is not defined

either define Test like this

<?php 
define("TEST", "key words to match", true); //here true is used for case-insensitive
if(isset($_GET['Keyword'])){
    if($_GET['Keyword'] == Test){
        header('Location: http://www.anotherURL.com');
        exit ();
    }
}
?>

or use it as a string

<?php 
if(isset($_GET['Keyword'])){
    if($_GET['Keyword'] == "Test"){ //qoutes " are necessary for string 
        header('Location: http://www.anotherURL.com');
        exit ();
    }
}
?>
Ahmed Raza
  • 39
  • 4
  • I'm not the down voter, but in your second example, what do you mean by "use it as a variable"? It's a string, not a variable. And the statement "commas are necessary for undefined constants" makes little sense. I'm guessing you're referring to the quotes `"`, but that has nothing to do with constants. Those are necessary for defining _strings_, or PHP will think you meant a constant. – M. Eriksson Oct 19 '22 at 16:59
  • @M.Eriksson i just updated those mistakes, thanks are pointing out – Ahmed Raza Oct 20 '22 at 07:17