0

I'm trying to use the query from a GET url to determine the content of a page. This is what I have (sentences edited for clarity):

    <?php 
//decalre variables
$title ='';
$welcome = '';
$params = '';

$params = $_SERVER['QUERY_STRING'];
echo $_SERVER['QUERY_STRING'];
echo $params;

if ($params='') {
    header('start.html') ;
} else {
    if ($params === "selection=republic") {
        //echo $params;
        //echo 'Republic';
        $title = "Private";
        $welcome = "Our .";
        $signoff = "The Republic will strike back!";
    } 
    else if ($params === "selection=rebels")    {
        //echo $params;
        //echo 'Rebels';
        $title = "Comrade";
        $welcome = "Hey comrade, welcome to the Underground Network!";
        $declaration="You see,o's!";
        $signoff = "Rebel!!";
    } 
    else if ($params === "selection=robots"){
        //echo $params;
        //echo 'Robots';
        $title = "Bit";
        $welcome = "Our data ";
        $declaration="Knowledge w.";
        $signoff = "ed now.";
    }
    else {
        echo 'There was an error - please go back.';
    }
}

The first echo shows the correct URL, but the comparison gets stuck at the third option. Help!

AliH
  • 87
  • 3
  • 8

2 Answers2

1

There are way better ways of parsing the query string than $SERVER['QUERY_STRING'], specifically you can use $_GET to access a specific parameter. Example: www.example.com?name=Dave&age=30... to get the name, you can do $_GET['name'] and it will return Dave. I think a better way to do this would be something like:

$selection = $_GET['selection'];
if (empty($selection)) {
    header('start.html') ;
}

else {
     $vars = array(
          'republic'=>array('title'=>'Private', 'welcome'=> 'Our .', 'declaration'=>'', 'signoff' => 'The Replublic will strike back'),
          'rebels'=>array('title'=>'Comrade', 'welcome' => "Hey comrade, welcome to the Underground Network!", 'declaration'=>"You see,o's!",'signoff' => "Rebel!!"),
          'robots'=>array('title'=>'Bit', 'welcome'=>'Our data', 'declaration'=>'Knowlegge W', 'signoff'=>'Ed now')
      );

      list($title, $welcome, $declaration, $signoff) = $vars[$selection];
}
dave
  • 62,300
  • 5
  • 72
  • 93
1

This comes from the triple = sign, that compares the value and the type. You should see the difference here.

I suggest you only use two equals and by the way, you could ease your code by using the $_GET['selection'] variable instead:

<?php 
//decalre variables
$title ='';
$welcome = '';
$params = '';

$params = $_SERVER['QUERY_STRING'];
echo $_SERVER['QUERY_STRING'];
echo $params;

if (!isset($_GET['selection']) { // Check whether selection is set 
    header('start.html') ;
} else {
    if ($_GET['selection'] == "republic") {
        //echo $params;
        //echo 'Republic';
        $title = "Private";
        $welcome = "Our .";
        $signoff = "The Republic will strike back!";
    } 
    else if ($_GET['selection'] == "rebels")    {
        //echo $params;
        //echo 'Rebels';
        $title = "Comrade";
        $welcome = "Hey comrade, welcome to the Underground Network!";
        $declaration="You see,o's!";
        $signoff = "Rebel!!";
    } 
    else if ($_GET['selection'] == "robots"){
        //echo $params;
        //echo 'Robots';
        $title = "Bit";
        $welcome = "Our data ";
        $declaration="Knowledge w.";
        $signoff = "ed now.";
    }
    else {
        echo 'There was an error - please go back.';
    }
}
Community
  • 1
  • 1
huguesval
  • 36
  • 5