简体   繁体   English

样式表的路径无法正常工作的PHP

[英]path of stylesheet not working php

im trying to link the stylesheet but for some reason I go to the page and its as if its not linked to the page (ie as if theres no stylesheet). 我试图链接样式表,但是由于某种原因,我转到该页面,好像它没有链接到页面一样(即好像没有样式表)。 This is the code for the page. 这是页面的代码。 Im new to this so i dont know if im giving enough information and nor can i think of anything else to put in. Thanks in advance 我是新来的人,所以我不知道我是否提供了足够的信息,我也想不出其他任何东西。

<?php
$path = $_SERVER['DOCUMENT_ROOT'];

echo "
<!DOCTYPE HTML>
<HTML>
    <head>
        <meta charset=\"utf-8\">
        <title>Home - Study Success</title>
        <!-- link to Styles -->
        <link rel=\"stylesheet\" href= \"$path/test/styles.css\">
    </head>
";

include_once ("$path/test/wrapper.php");

also if i view the source page, copy and paste the path of the stylesheet in a new tab, the stylesheet opens up but if i click and select open in new tab i get a blank page. 另外,如果我查看源页面,将样式表的路径复制并粘贴到新选项卡中,则样式表会打开,但是如果我单击并选择在新选项卡中打开,则会出现空白页面。 Just opened it on firefox, viewed source page and clicked on the path of the stylesheet, this is what i got, i still have no idea how to fix this!? 刚刚在firefox上打开它,查看了源页面并单击了样式表的路径,这就是我得到的,我仍然不知道如何解决此问题!

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /test/.C:/Program Files       (x86)/Apache/htdocs/test/styles.css
on this server.</p>
</body></html>

The root of your web-server on the client side is the $_SERVER['DOCUMENT_ROOT'] folder on the server side. 客户端的Web服务器的根是服务器端的$_SERVER['DOCUMENT_ROOT']文件夹。

So you can just use: 因此,您可以使用:

<link rel="stylesheet" href="/test/styles.css">

(without including $path ) (不包括$path

$_SERVER['DOCUMENT_ROOT'] gives the root path on the server, not the URL. $_SERVER['DOCUMENT_ROOT']给出服务器上的根路径,而不是URL。 Try using $_SERVER['SERVER_NAME'] instead 尝试改用$_SERVER['SERVER_NAME']

Oh I see what you mean, don't rely on $_SERVER['DOCUMENT_ROOT']; 哦,我明白您的意思了,不要依赖$_SERVER['DOCUMENT_ROOT']; that will look in the filesystem: 将在文件系统中查找:

<?php
// this can go in a config file that you can include
define('CSS_PATH', realpath(dirname(__FILE__).'/css'));

echo '
<!DOCTYPE HTML>
<HTML>
    <head>
        <meta charset="utf-8">
        <title>Home - Study Success</title>
        <!-- link to Styles -->
        <link rel="stylesheet" href="'.CSS_PATH.'/test/styles.css">
    </head>
';

include_once (CSS_PATH."/test/wrapper.php");

Now im not sure why you have style.css and wrapper.php in the same directory but in this example I assume you placed it in a directory called css . 现在我不确定为什么在同一目录中有style.csswrapper.php ,但是在此示例中,我假设您将其放置在名为css的目录中。

So you might need to tweak this, but you get the idea... 所以您可能需要对此进行调整,但是您知道了...

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

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