基于服务器URL路径的条件标头源 [英] conditional header source based on server url path

查看:97
本文介绍了基于服务器URL路径的条件标头源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我想在CMS上为每种内容类型( $ categoryId )加载自定义头文件。例如,如果url为 /?action = archive& categoryId = 1,我希望它包含我的 header_medicine.html文件。

Problem: I want to load a custom header file for each content type ($categoryId) on my CMS. For example, if the url is "/?action=archive&categoryId=1", I want it to include my "header_medicine.html" file.

我绝对是php noob,但是我尝试遵守这个论坛并使用这篇关于基于url的条件代码的帖子,但存档页面仍从我的'else加载' 健康)状况。

I'm definitely a php noob, but I've tried to respect this forum and solve my question using the tips at this post about conditional code based on url, but the archive page still loads from my 'else' condition.

这是代码:

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

感谢您考虑我的问题!

Thanks for considering my question!



更新:解决方案

有趣的是,这是我上面发布的代码问题的有效解决方案(使用简化的fileysystem语法)。我没有使用@Michael在下面的代码中推荐的isset函数。感谢所有提供建议的人,我现在对php有了一个线索。

For anyone who is interested, here is the working solution to the code problem I posted above (with simplified fileysystem syntax). I didn't use the isset function that @Michael recommended in his code below. Thanks to everyone who offered suggestions, I'm now one step closer to having a clue about 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";
    }
?>


推荐答案

在PHP中实际上非常简单。您只需要通过$ _GET ['categoryId'] 的内容。 variables.get.php rel = nofollow> $ _ GET [] 超全局数组。 parse_url()及其表亲 parse_str()会正确解析出一个URL,在您的情况下,您可以本来希望查看的是 $ archive_url ['query'] ,但这都是不必要的-您需要的信息在 $ _ GET

You're taking the long way around something that is actually quite simple in PHP. You only need to check the contents of $_GET['categoryId'] via the $_GET[] superglobal array. parse_url() and its cousin parse_str() will parse correctly parse out a URL, and in your case the part you would have wanted to look at was $archive_url['query'], but this is all unnecessary -- the information you need is in $_GET.

if (isset($_GET['categoryId']) {
// If this also depends on action=archive, use
// if (isset($_GET['categoryId']) && isset($_GET['action']) && $_GET['action'] == 'archive')
  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('templates/include/header.html');
  }
}

现在,我怀疑您实际上是要包含 /header_science.htm这样的文件l include()调用文件系统路径,因此,除非您的文档根也是服务器文件根路径 / ,否则这些可能是不是正确的包含路径。

Now, I find it suspicious that you actually mean to include files like /header_science.html. include() calls filesystem paths, so unless your document root is also the server file root path /, these are probably not the correct inclusion paths.

您可能会寻找

include($_SERVER['DOCUMENT_ROOT'] . '/header_science.html');

这篇关于基于服务器URL路径的条件标头源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆