doutan5844 2018-09-08 22:58
浏览 84
已采纳

设置常量以重用HTML

I am trying to set a WWW_ROOT in order to reuse HTML code in different pages. This is an example of users.php using a header, where the CSS files are not found - 404 (Not Found) and I got Notice: Undefined variable: project_end in /Applications/XAMPP/xamppfiles/htdocs/lpweb/assets/php/initialize.php on line 11

Folder structure

>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条回答 默认 最新

  • dsrnwngq411594 2018-09-09 01:08
    关注

    You are mixing $public_end and $project_end

    Also after reviewing your code from GitHub I believe this will work for you.

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

    Alternatively, you can use the relative path to refer to CSS files which will make your life easy

    <link rel="stylesheet" media="all" href="assets/css/bootstrap.min.css" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b