简体   繁体   English

无法生成 CSS 与 php 的链接

[英]Can't generate CSS link with php

I am using PHP to generate layout for some pages, the trouble is, it just don't seem to be able to find my custom CSS file.我正在使用 PHP 为某些页面生成布局,问题是,它似乎无法找到我的自定义 CSS 文件。 Here is the code that I am using:这是我正在使用的代码:

<link rel="stylesheet" href="' . $_SERVER['DOCUMENT_ROOT'] . '/CSS/jokesStyle.css">

Note: it is a php string in which I am using it.注意:这是一个 php 字符串,我正在其中使用它。 The CSS file is located in htdocs/CSS. CSS 文件位于 htdocs/CSS 中。 Here is the path generated in browser:这是在浏览器中生成的路径:

C:/xampp/htdocs/CSS/jokesStyle.css

no CSS rules from this file applies to the generated page.此文件中没有 CSS 规则适用于生成的页面。 Thanks in advance!提前致谢!

Use the following code使用以下代码

<?php  
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){  
         $url = "https://";   
    }else{  
         $url = "http://";
    }
    $url.= $_SERVER['HTTP_HOST']; 
    $url .= "/CSS/jokesStyle.css";
    echo "<link rel='stylesheet' href='{$url}'>";
?>

You can define like你可以定义像

define('ROOTINDEX','https://localhost/Your site.com');

define('ROOTCSS','http://localhost/assets/css');

and where you want以及你想要的地方

<?php echo ROOTINDEX ?>/url
<?php echo ROOTCSS ?>/file.css

And For live and local you can define like this对于现场和本地,您可以这样定义

if($_SERVER['HTTP_HOST']=="localhost")
{
define('ROOTINDEX','http://localhost/your site.com');
define('ROOTBIMG','http://localhost/your site.com/upload/banner-images');
define('ROOTLOGO','http://localhost/yoursite/upload/logo-images');  
define('ROOT','http://localhost/yoursite.com/admin');
///define('HOST', $_SERVER['SERVER_NAME']);
$server = 'localhost';
$user = 'root';
$password = ''; 
$database = 'localdb name';
$port = '3308';
}else{
define('ROOTINDEX','http://yoursite.com');
define('ROOTBIMG','http://yoursite.com/upload/banner-images');
define('ROOTIMG','http://yoursite.com/upload/post-images');
define('ROOTPROIMG','http://Your site.com/upload/product');
define('ROOT','http://Your site.com/admin');
//define('HOST', $_SERVER['SERVER_NAME']);
$server = 'localhost';
$user = 'username';
$password = 'Password';
$database = 'dbname';
/*$port = '3308';*/
}

First of all check that do you use echo command correctly.首先检查你是否正确使用了echo命令。

Your code can be like that你的代码可以是这样的

echo '<link rel="stylesheet" href="' . $_SERVER['DOCUMENT_ROOT'] . '/CSS/jokesStyle.css">';

or like that或那样

<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/CSS/jokesStyle.css">

When you open developer tools and see your css file is not found with error code 404 this means you are having some issue with path.当您打开开发人员工具并看到您的 css 文件未找到且错误代码为404时,这意味着您的路径存在问题。

So check var_dump($_SERVER['DOCUMENT_ROOT']);所以检查var_dump($_SERVER['DOCUMENT_ROOT']); path is that giving you what you want.道路是给你你想要的。

If C:/xampp/htdocs/CSS/jokesStyle.css this path is working so you need to be sure $_SERVER['DOCUMENT_ROOT'] must give you that C:/xampp/htdocs .如果C:/xampp/htdocs/CSS/jokesStyle.css此路径有效,那么您需要确保$_SERVER['DOCUMENT_ROOT']必须给您C:/xampp/htdocs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM