设置常量以重复使用HTML

时间:2018-09-08 22:58:29

标签: javascript php html node.js html5

我试图设置WWW_ROOT以便在不同页面中重用HTML代码。这是一个使用标头的users.php的示例,在该标头中找不到CSS文件-404(未找到),我得到了Notice: Undefined variable: project_end in /Applications/XAMPP/xamppfiles/htdocs/lpweb/assets/php/initialize.php on line 11

文件夹结构

>localhost
  >lpweb
       >index.php
       >pages
          >users.php
       >assets
          >php
             >initialize.php
             >header.php
          >css
             >bootstrap.min.css

initialize.php

<?php
  define("PHP_PATH", dirname(__FILE__));
  define("ASSETS_PATH", dirname(PHP_PATH));

  $public_end = strpos($_SERVER['SCRIPT_NAME'], '/lpweb') + 7;
  $doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $project_end); //line 11
  define("WWW_ROOT", $doc_root);
?>

header.php

<link rel="stylesheet" media="all" href="<?php echo WWW_ROOT . 'assets/css/bootstrap.min.css'; ?>"> />

users.php

<?php require_once('../assets/php/initialize.php'); ?>
<?php include(PHP_PATH . '/header.php'); ?>

1 个答案:

答案 0 :(得分:0)

您正在混合使用$ public_end和$ project_end

另外,在从GitHub查看您的代码后,我相信这将对您有用。

heroku bash run

或者,您可以使用相对路径来引用CSS文件,这将使您的生活变得轻松

define("WWW_ROOT",$_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].rtrim($_SERVER['REQUEST_URI'],'/'));