链接我的文件PHP问题


Problems linking my files PHP

连接profile.php有问题

主文件夹

index.php
page1.php
page2.php
page3.php
server folder
css folder
include folder
user folder

服务器文件夹包含

connect.php

css文件夹包含

stylesheet.css

include folder contains

header.php
footer.php

用户文件夹包含

profile.php

所有的文件都有

include ROOT.'include/header.php';
include ROOT.'include/footer.php';

inside my header.php

<!doctype HTML>
<html>
<head>
     <link rel="stylesheet" type="text/css" href="css/stylesheet.css">
</head>
<body>

i used

chdir('maindirectory');
define('ROOT', getcwd().DS);

一切工作,除了我的profile.php似乎不能加载我的CSS文件。我将个人资料分成用户的原因是因为我使用htaccess来删除'.php',我想让用户通过用户/'username'进行搜索。

如果我从来没有分开,我将有问题去其他页面,如example.com/page2,它会搜索用户,而不是去page2。

尝试使用$_SERVER['DOCUMENT_ROOT']而不是ROOT来包含文件,或者将ROOT定义为$_SERVER['DOCUMENT_ROOT']

您不需要使用chdir和define来知道工作文件夹。你可以使用__DIR__常量返回工作目录(参见include前的斜杠,__DIR__不返回最后一个斜杠):

include __DIR__ . '/include/header.php';
include __DIR__ . '/include/footer.php';

在profile.php __DIR__有根/用户的值。这意味着你需要做:

include __DIR__ . '/../include/header.php';
include __DIR__ . '/../include/footer.php';

如果你的PHP版本不支持__DIR__,你可以使用dirname(__FILE__)代替:

include dirname(__FILE__) . '/include/header.php';
include dirname(__FILE__) . '/include/footer.php';

如果你不知道或怀疑你所在的目录,总是可以回显:

echo __DIR__;