加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 基于服务器URL路径的条件头源

发布时间:2020-12-13 22:34:18 所属栏目:PHP教程 来源:网络整理
导读:问题:我想在CMS上为每种内容类型($categoryId)加载自定义头文件.例如,如果网址是“/?action = archive categoryId = 1”,我希望它包含我的“header_medicine.html”文件. 我绝对是一个PHP菜鸟,但我试图尊重这个论坛,并使用 this post about conditional co
问题:我想在CMS上为每种内容类型($categoryId)加载自定义头文件.例如,如果网址是“/?action = archive& categoryId = 1”,我希望它包含我的“header_medicine.html”文件.

我绝对是一个PHP菜鸟,但我试图尊重这个论坛,并使用 this post about conditional code based on url的提示解决我的问题,但存档页面仍然从我的’其他’条件加载.

这是代码:

<?php $archive_url = parse_url($_SERVER['REQUEST_URI']);

if ($archive_url['path'] == "/?action=archive&categoryId=1")
    include "header_medicine.html"; 

    elseif ($archive_url['path'] == "/?action=archive&categoryId=2")
    include "header_science.html"; 

    elseif ($archive_url['path'] == "/?action=archive&categoryId=3")
    include "header_other.html"; 

    else 
    include "header.html";
?>

谢谢你考虑我的问题!

?更新:解决方案

对于任何感兴趣的人,这里是我上面发布的代码问题的工作解决方案(使用简化的文件系统语法).我没有使用@Michael在下面的代码中推荐的isset函数.感谢所有提出建议的人,我现在离PHP有了一些线索.

<?php switch ($_GET['categoryId']) {
    case 1:
    include "header_medicine.html";
    break;
    case 2:
    include "header_science.html";
    break;
    case 3:
    include "header_other.html";
    break;
    default:
    include "header.html";
    }
?>

解决方法

你可以通过$_GET,$_POST和 others访问PHP中的参数.所以$_GET [‘action’]将给出动作类型归档,$_GET [‘categoryId’]将给你1.

所以你可以这样做:

<?php
  switch ($_GET['categoryId']) {
    case "1":
      include "/header_science.html"; 
      break;
    case "2":
      include "/header_other.html"; 
      break;
    default:
      include "templates/include/header.html";
  }
?>

http://php.net/manual/en/control-structures.switch.php

你的示例代码不起作用,因为$archive_url [‘path’]只给你路径/.从php手册看一下这个例子:

http://php.net/manual/en/function.parse-url.php

<?php
  $url = 'http://username:password@hostname/path?arg=value#anchor';
  print_r(parse_url($url));
  echo parse_url($url,PHP_URL_PATH);
?>

上面的例子将输出:

Array
(
  [scheme] => http
  [host] => hostname
  [user] => username
  [pass] => password
  [path] => /path
  [query] => arg=value
  [fragment] => anchor
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读