0

我正在尝试使用来自 GET url 的查询来确定页面的内容。这就是我所拥有的(为清楚起见,对句子进行了编辑):

    <?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.';
    }
}

第一个回显显示正确的 URL,但比较卡在第三个选项上。帮助!

4

2 回答 2

1

解析查询字符串有比 更好的方法$SERVER['QUERY_STRING'],特别是您可以使用它$_GET来访问特定参数。示例: www.example.com?name=Dave&age=30... 要获取名称,您可以这样做$_GET['name'],它会返回Dave. 我认为更好的方法是:

$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];
}
于 2013-04-15T08:16:23.413 回答
1

这来自三重 = 符号,它比较值和类型。您应该在这里看到不同之处

我建议你只使用两个等号,顺便说一下,你可以使用 $_GET['selection'] 变量来简化你的代码:

<?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.';
    }
}
于 2013-04-15T08:17:53.457 回答