2013-04-15 47 views
0

我试图使用GET url中的查询来确定页面的内容。 这是我(编辑为清楚起见句子):比较PHP中的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,但比较卡在第三个选项。 帮助!

+5

我相信第一个'如果($ PARAMS = '')'应该是'=='。 – wroniasty

+1

为什么不使用'$ _GET ['selection']'? –

+0

if((空($变量))而不是if($ variable =='')...用于检测是否没有值或空白((和0也是如此注意整数))更好。 – aleation

回答

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

请更正错误和格式:) – mkjasinski

+0

那里亚去,格式化和固定 – dave

+0

和'$ SERVER ['QUERY_STRING']'? – mkjasinski

1

这来自于三重=标志,用于比较值和类型。你应该看到the difference here

我建议你只使用两个平等的,顺便说一下,你可以通过使用$ _GET [“选择”]变量,而不是减轻你的代码:

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