PHP: 无法引用css文件?
伊谢尔伦
伊谢尔伦 2017-04-10 16:47:30
[PHP讨论组]

最近学PHP,到第三章的时候卡住了。我引用了一个HTML模板,但是这个模板无法引用css文件,下图是这个HTML模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><?php echo $page_title; ?></title>    
    <link rel="stylesheet" href="../ch03/includes/style.css" type="text/css" media="screen" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
    <p id="header">
        <h1>Your Website</h1>
        <h2>catchy slogan...</h2>
    </p>
    <p id="navigation">
        <ul>
            <li><a href="index.php">Home Page</a></li>
            <li><a href="calculator.php">Calculator</a></li>
            <li><a href="#">link three</a></li>
            <li><a href="#">link four</a></li>
            <li><a href="#">link five</a></li>
        </ul>
    </p>
    <p id="content"><!-- Start of the page-specific content. -->
<!-- Script 3.2 - header.html -->

但是,我通过在位于表头中的<style>标签里加入:

<?php include ('./includes/style.css'); ?>

它又工作了。通过审查源代码发现所有CSS代码都集中到这个HTML文件来了。我知道这样不好,但有没有办法解决?

还有,我在网站根目录里放入 *.html 文件提示 File not found.(能解析*.php)。

Nginx配置文件:

server {
    listen 80 default_server;
    root /var/www/html;
    index index.html index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

=====================UPDATE on 2016.02.16 00:56===================

这是修改后的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><?php echo $page_title; ?></title>    
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <style>
    <?php include ('./includes/style.css'); ?>
    </style>
    <!-- <link rel="stylesheet" href="../ch03/includes/style.css" type="text/css" media="screen" /> -->
</head>

对,它能工作:

我的项目文件结构:

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(7)
迷茫

相对路径写错了吧 试试把路径改成你php引用的一样 还有把link那一行往下挪

高洛峰

css那个你看下是不是路径问题

相对路径

我看你模版是两个点,也就是上一级目录

style标签下则是一个点

ringa_lee

对了,用Link的方式引用这个路径./includes/style.css 还是不行。

大家讲道理

为何要用PHP引用css文件呢?这样做不科学啊

PHP中文网

css引用都是用
<link rel="stylesheet" href="../ch03/includes/style.css" type="text/css" media="screen" />
这种形式的 = =,你那种是php的include文件,css不用那种的

巴扎黑

路径的问题吧,你是在 header.html 里面引入css出问题的?
应该是 ./style.css
或者是../includes/style.css 这样吧

怪我咯

使用绝对路径试试 可以生效再改成相对的

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号