使用URL变量的PHP切片

时间:2017-07-23 18:40:30

标签: php

我想使用PHP创建动态标题。我的网址如下所示:

domain.com/results?cityid=3432&type=1

我希望能够将cityid = 3432定义为“纽约市”并输入= 1作为“Condos”等,以便使用我定义的URL变量创建有意义的标题标签。

即。 domain.com/results?cityid=3432&type=1会生成标题标签“纽约市公寓”

要定义数百个不同的变量,因此最好使用外部文件。

我在这个网站上看到了另一种解决方案,但它被标记为易受攻击。

1 个答案:

答案 0 :(得分:0)

如果城市ID中包含存储在数据库中的相应城市名称(类型相同),则非常简单:

  1. 您将使用get和query从您的网址中提取网址中的ID cities-table获取城市名称并将其保存在变量中。
  2. 您为该类型执行此操作。
  3. 您可以根据需要合并两者,并将其用作标题标记值。
  4. 如果您不使用数据库,则需要将所有组合存储在文件中的数组中并包含它们。之后,您将获得标题标记值:

    <?php
    
    //Save this into a seperate file e.g. titlecodes.php
    //Modify those arrays until they meet your requirements
    $cities = array(1=> "Washington", 2 => "Los Angeles");
    $types = array(1 => "House", 2 => "Penthouse", 3=> "Industrial");
    
    // Include titlecodes.php in your script 
    $type = '';
    $city = '';
    if(isset($_GET["city_id"]) && array_key_exists($_GET["city_id"], $cities)) 
        $city = $cities[$_GET["city_id"]];
    if(isset($_GET["type"]) && array_key_exists($_GET["type"], $types)) 
        $type = $types[$_GET["type"]];
    
    $titleTagValue = $city . " " . $type;
    
    //put it in header title 
    //<header>
    //<title><php echo $titleTagValue </title>
    //</header>