Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果导航到url,则不会读取PHP-CSS_Php - Fatal编程技术网

如果导航到url,则不会读取PHP-CSS

如果导航到url,则不会读取PHP-CSS,php,Php,我的网站使用URL段来创建动态页面。 我基本上是加载php文件的内容,而不是重定向到它们。 一切正常,导航到CSS未加载的第二个URL段 请允许我详细说明: mydomain.com/foo将“root/somedirectory/foo/index.php”加载到my main index.php中的my mydomain.com/foo/bar将“root/somedirectory/foo/bar.php”加载到my main index.php中的my if(!urlSegment(1)

我的网站使用URL段来创建动态页面。
我基本上是加载php文件的内容,而不是重定向到它们。 一切正常,导航到CSS未加载的第二个URL段

请允许我详细说明: mydomain.com/foo将“root/somedirectory/foo/index.php”加载到my main index.php中的my

mydomain.com/foo/bar将“root/somedirectory/foo/bar.php”加载到my main index.php中的my

if(!urlSegment(1)) include("home.php");
else{
    $target = strtolower(urlSegment(1));
    $targetFolder = FILE_PATH.$target."/";
    $fileName = !urlSegment(2) ? "index.php" : strtolower(urlSegment(2)).".php";
    $fileTarget = $targetFolder.$fileName;
    if(file_exists($fileTarget)) include($fileTarget);
    else{
        echo 'Page not found ('.$fileTarget.')';
    }
}
问题是:当我转到第二个URL(URL中有条)时,main index.php中的CSS不再被加载,控制台会收到错误的垃圾邮件

这很奇怪,因为它的逻辑与我不添加bar时的逻辑基本相同(因此加载index.php)

错误:

我的代码(main index.php)

URLSEMENT函数只是将url的一部分作为字符串获取。
如果你有“www.stackoverflow.com/foo/bar/donut”foo=1,bar=2,donut=3

提前谢谢

编辑后 完整(main)index.php如下所示:

<html>
   <head>
      <!-- Bunch of CSS / Javascript Links -->
   </head>
   <body>
      <?php /*php The code above */ ?>
   </body>
</html>
$path = (explode('?', $_SERVER['REQUEST_URI']))[0];  // request path
$mime = array_pop((explode('.', $path)));  // file extension

if ($mime == 'php')
{
    // ... your code here
}
else
{
    if (!is_readable($path))
    {
        $fail = 'HTTP/1.0 404 Not Found';
        header(!file_exists($path) ? $fail : 'HTTP/1.0 403 Forbidden');
        exit;
    }

    ob_get_clean(); // clear output buffer that could cause issues

    header('HTTP/1.0 200 OK');

    // ... send appropriate headers here, according to mime & size

    readfile($path);
    exit;
}

“bar.php”基本上包含普通的HTML,这些HTML将包含在
标记中。它使用
标记中CSS链接的样式。 请记住,一个URL段就像一个符咒,第二个会破坏它

这会在控制台中为每个.css文件弹出:
资源被解释为样式表,但使用MIME类型text/html传输

当我导航到一个URL段时,通过控制台看到的文件夹结构(应该是这样的):

当我导航到两个URL段时通过控制台看到的文件夹结构(好像不应该这样):

代码的其余部分是个谜,但作为一个快速解决方案,请尝试以下方法:

<html>
   <head>
      <!-- Bunch of CSS / Javascript Links -->
   </head>
   <body>
      <?php /*php The code above */ ?>
   </body>
</html>
$path = (explode('?', $_SERVER['REQUEST_URI']))[0];  // request path
$mime = array_pop((explode('.', $path)));  // file extension

if ($mime == 'php')
{
    // ... your code here
}
else
{
    if (!is_readable($path))
    {
        $fail = 'HTTP/1.0 404 Not Found';
        header(!file_exists($path) ? $fail : 'HTTP/1.0 403 Forbidden');
        exit;
    }

    ob_get_clean(); // clear output buffer that could cause issues

    header('HTTP/1.0 200 OK');

    // ... send appropriate headers here, according to mime & size

    readfile($path);
    exit;
}
其思想是简单地“服务”不是“php”文件的任何其他文件


更新 上面的“修复”将有助于隔离处理不同文件类型的条件


图像中显示的错误可能是由JavaScript输出中的PHP错误引起的,这也可能是导致CSS未按预期显示的原因。

我不知道这将如何修复它?你能详细说明一下吗?我不知道你剩下的代码是什么样子的,但似乎你的逻辑试图在所有文件类型上运行;因此,将您的逻辑封装在一个只适用于“php”文件的条件下,而只适用于“服务”其他文件(如CSS、HTML或字体和图像)就可以了。。。尽管如此,这只是一个示例,在任何必要的地方或在代码中适当的地方修改和使用它。如果我完全错了,请更新您的问题并分享更多的代码,并解释流程,因为这将有助于理解错误。答案已更新,即使如此,要调试您的代码,请尝试注释您的PHP代码,并逐步对其进行相应的联合国评论。这将有助于隔离问题。我再次更新了我的问题,我在控制台中发现了一个奇怪的事件,关于两个URL中的文件夹结构