【问题标题】:PHP Server URL or Path to dynamically set CSS file动态设置 CSS 文件的 PHP 服务器 URL 或路径
【发布时间】:2015-07-15 01:59:52
【问题描述】:

在包含 PHP 页面时我的 css 文件有一些问题,所以我需要在此处设置完整的服务器路径,如下所示:

    <link rel="stylesheet" type="text/css" 
href="<?php echo $_SERVER["REQUEST_URI"];?>/css/cssfile.css">

问题是css文件不在根目录下而是这样:

domain.com / folder / subfolder / css / cssfile.css

因为我运行相同的设计副本并且文件夹和子文件夹的名称不同,所以我想设置 css 文件的完整路径/绝对路径)但在 PHP 中以某种方式

我尝试了上面的示例,但无法正常工作。我需要的是类似

     <link rel="stylesheet" type="text/css" 
    href="<?php echo $_SERVER["
GET CURRENT DOMAIN / FOLDER / SUBFOLDER"];?>/engine/default.css">

有没有办法做到这一点?

【问题讨论】:

    标签: php html css


    【解决方案1】:

    下面的代码将为您提供http://example.comhttps://example.com

    $domain = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
    

    现在您可以附加静态路径了。

    $css_url = $domain . '/my/dir/css/cssfile.css';
    

    希望这会有所帮助。谢谢!

    【讨论】:

      【解决方案2】:

      声明一个变量/常量为

      $css_path = '../../tosuit';
      

      然后在你的脚本中使用它

      <link rel="stylesheet" type="text/css" 
      href="<?php echo $css_path;?>/engine/default.css">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-29
        • 2017-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        • 1970-01-01
        相关资源
        最近更新 更多