Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用URL变量的PHP平铺_Php - Fatal编程技术网

使用URL变量的PHP平铺

使用URL变量的PHP平铺,php,Php,我想使用PHP创建动态标题。我的URL如下所示: domain.com/results?cityid=3432&type=1 我希望能够将cityid=3432定义为“纽约市”,type=1定义为“公寓”等,以便使用我定义的URL变量创建有意义的标题标记 i、 e.domain.com/results?cityid=3432&type=1会生成一个标题标签,上面写着“纽约市公寓” 将有数百个不同的变量需要定义,因此最好使用外部文件 我在这个网站上看到了另一个解决方案,但它被标记为易受攻击 如果将

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

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

我希望能够将cityid=3432定义为“纽约市”,type=1定义为“公寓”等,以便使用我定义的URL变量创建有意义的标题标记

i、 e.domain.com/results?cityid=3432&type=1会生成一个标题标签,上面写着“纽约市公寓”

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


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

如果将城市ID及其对应的城市名称(类型也一样)存储在数据库中,则非常简单:

  • 您将使用get从url中提取id,并根据您的 cities表获取城市名称并将其保存在变量中
  • 你这样做的类型
  • 您可以根据自己的喜好将两者结合起来,并将其用作标题标记值 如果不使用数据库,则需要将所有组合存储在文件中的数组中,并将其包括在内。之后,您将获得标题标签值:

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

    标题中有输入错误,未定义时应添加404;普苏尔,你说得对。或者像“Heeelpp,我不退出”这样的消息:)我没有使用数据库。但是,我可以创建一个包含两列的文本文件。列A将包含在URL中找到的变量,列B将包含我希望在标题中打印的英语等效项。URL字符串可以很长,并且包含许多不同的变量。我想在一个文本文件中定义它们,并让PHP查找任何匹配项。可能有数十万种潜在的组合。i、 e.城市ID=3432,类型=1,minbed=2,maxprice=500000。我想在标题标签上印上“纽约市公寓2间卧室50万美元”。