dongren4147 2017-03-10 20:15
浏览 54
已采纳

PHP包含和CSS

In my website, I'd like to use the PHP include line within the index.php page to add in important sections like the header and footer.

Now, the folder structure is as follow:

/index.php

/css/style.css

/includes/header.php

/includes/footer.php

Keep in mind I am working in a localhost environment.

Now my question to you is how do I properly reference my CSS file in the index.php and header.php? Should I use a config.php file? I'd like to avoid using absolute paths if that's possible.

Currently, my CSS file is read and displays properly, however, when I make changes, the CSS does not display the changes made and keeps the original unchanged file.

index.php

<head>
    <link rel="stylesheet" href="css/style.css" type="text/css">
</head>

<body>
  <?php include 'includes/header.php';?>
  <div class="container">
    <div class="inner-container">
      <?php include 'includes/javascript.php';?>
    </div>

header.php

<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
  <header>
      <div id="logo">
        <div class="logo-container">
          <img src="img/final_logo_400px.png" alt="">
        </div>
      </div>
      <nav>
        <ul>
  • 写回答

1条回答

  • donglian1953 2017-03-10 20:25
    关注

    Simply use a relative link no? ie:

    <link href="/css/style.css" rel="stylesheet" type="text/css" />
    

    I presonally do the following which checks whether the file is local or remote, and versions the file using its modification time if it is remote (that check fails locally):

     $url = $_SERVER['DOCUMENT_ROOT'];
     $parts = explode('/',$url);
     $base = $parts[0];
     if ($base == 'C:') { ?>
       <link href="/css/style.css" rel="stylesheet" type="text/css" />
     <?php } else { ?>
       <link href="/css/style.css?v=<?php echo filemtime($basepath."/css/style.css")?>" rel="stylesheet" type="text/css" />
     <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况