所以我正在整理一个简单的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>
答案 0 :(得分: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
答案 1 :(得分: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;
}
?>
答案 2 :(得分: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>