链接我的文件PHP的问题

时间:2016-11-06 09:43:26

标签: php .htaccess

我在连接profile.php时遇到问题

主文件夹

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

服务器文件夹包含

connect.php

css文件夹包含

stylesheet.css

包含文件夹

header.php
footer.php

用户文件夹包含

profile.php

所有文件都有

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

在我的header.php里面

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

我用过

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

一切正常,除了我的profile.php似乎无法加载我的css文件。因为我使用htaccess来移除&#39; .php&#39;我希望允许用户搜索用户/&#39;用户名&#39;同样。

如果我从不分开,我会在转到其他网页时遇到问题,例如example.com/page2它会搜索用户而不是转到第2页。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

在profile.php __DIR__中有root / user值。这意味着你需要这样做:

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__;