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!

4
  • 5
    I believe the first if ($params='') should be ==. Commented Apr 15, 2013 at 8:06
  • 1
    Why not use $_GET['selection']? Commented Apr 15, 2013 at 8:07
  • also it's much better to use if(empty($variable)) instead of if($variable == '') ... empty detects if there's no value or whitespaces, (and 0's too so be careful with ints) Commented Apr 15, 2013 at 8:11
  • Thanks all - the "==" fixed it and using is simpler Commented Apr 15, 2013 at 8:18

2 Answers 2

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.';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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];
}

1 Comment

and $SERVER['QUERY_STRING']?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.