0

所以我正在整理一个简单的 php 页面,它将根据输入的 url 显示不同的内容。出于某种原因,它似乎通过整个 if 语句而不做任何事情。

<?php
    if(array_key_exists('offer', $_GET)) {
        if( $_GET == "ncp" ){
            $title = 'Exclusive Offer from NCP';
        } elseif($_GET == "rt") {
            $title = 'Exclusive Offer from RT';
        } elseif($_GET == "oo") {
            $title = 'Exclusive Offer from OO';
        }
    } else {
        $title = 'Check Out These Exclusive Offers!';
    }
    $title = strip_tags($title);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title; ?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script type="text/javascript">

</script>
</head>
4

3 回答 3

6

你应该使用$_GET['offer'] == "foo"而不是$_GET == "foo"

更好的是,将来使用严格比较来确保您比较相同的类型(在本例中为字符串):===. 这是因为 PHP 倾向于转换类型,这既有用又烦人(参见http://php.net/manual/en/language.types.type-juggling.php):

$foo = "0";  // $foo is string (ASCII 48)
$foo += 2;   // $foo is now an integer (2)
$foo = $foo + 1.3;  // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs";     // $foo is integer (15)

$_GET: http: //php.net/manual/en/reserved.variables.get.php

比较运算符: http: //php.net/manual/en/language.operators.comparison.php

于 2013-09-12T15:08:35.087 回答
1

另一种方法 试试这个 我总是强调清除通过 $_POST、$_GET 或任何外部源传递的任何变量,这就是我使用 filter_var 的原因。

<?php

    if(isset($_GET['offer'])) {     
      //lets sanitize $_GET['offer'] if it exist since its passed via url
      $offer = filter_var($_GET['offer'], FILTER_SANITIZE_STRING);
    } else {
      $offer = NULL;
    }

    switch ($offer) {
    case 'ncp':
      $title = 'Exclusive Offer from NCP';
    break;
    case 'rt':
      $title = 'Exclusive Offer from RT';
    break;
    case 'oo':
      $title = 'Exclusive Offer from OO';
    break;
    default:
      $title = 'Check Out These Exclusive Offers!';
    break;
    }

?>
于 2013-09-12T15:43:01.403 回答
0

这就是在 php 中使用 $_GET 的方法。试试看

<?php
if(isset($_GET['offer'])){
    if( $_GET['offer'] == "ncp" ){
    $title = 'Exclusive Offer from NCP';
    }
    elseif( $_GET['offer'] == "rt" ){
    $title = 'Exclusive Offer from RT';
    }
    elseif( $_GET['offer'] == "oo" ){
    $title = 'Exclusive Offer from OO';
    }
} 
else {
    $title = 'Check Out These Exclusive Offers!';
}
$title = strip_tags($title);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title; ?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script type="text/javascript">
</script>
</head>
于 2013-09-12T15:30:56.773 回答