简体   繁体   English

尝试根据PHP中的URL显示标题

[英]Trying to display a title based on the url in PHP

So i'm putting together a simple php page which will display different content based on what url is put in. For some reason it seems to make it through the whole if statement without doing anything. 因此,我将整理一个简单的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>

You should use $_GET['offer'] == "foo" instead of $_GET == "foo" 您应该使用$_GET['offer'] == "foo"而不是$_GET == "foo"

Better yet, use strict comparison in the future to make sure you are comparing the same types (in this case strings): === . 更好的是,将来使用严格比较来确保您正在比较相同类型(在这种情况下为字符串): === This is because PHP has the tendency to cast types around, which can be both useful and annoying (see http://php.net/manual/en/language.types.type-juggling.php ): 这是因为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 $_GEThttp : //php.net/manual/en/reserved.variables.get.php

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

Another way of doing it Try this I always make it a point to clean any variable passed via $_POST, $_GET or any external source thats why I use filter_var. 尝试执行此操作的另一种方法我总是将其作为清除通过$ _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;
    }

?>

This is how you can use $_GET in php. 这就是您可以在php中使用$ _GET的方式。 Give it a try 试试看

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM