dtnat7146 2015-08-12 07:21
浏览 30
已采纳

无法加载标头php文件的样式和脚本[重复]

This question already has an answer here:

This is how my header.php looks like;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="headerStyle.css">
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js"></script>
    <script src="headerScript.js"></script>
<?php

if( strpos($_SERVER['PHP_SELF'], 'index') ){
    echo '<title>My website title</title>';
    echo '<link rel="stylesheet" type="text/css" href="index.css">';
    echo '<script src="index.js"></script>';
}
if( strpos($_SERVER['PHP_SELF'], 'index') ){
    echo '<title>Contact Us</title>';
}//And goes on like this..

?>

</head>

header.php,headerStyle.css,headerScript.js are in same folder : mysite/header/

And this how my index.php looks like;

<?php 
include 'header/header.php';  
    //And some other irrelevant stuff (<body> etc.)
?>

My index file's path is mysite/

The problem is when I view the index page it cannot load headerStyle.css and headerScript.js.

What is the best way to manage that?I'm trying to make a solid,dynamic header.php so i can include it for every page in my site.

</div>
  • 写回答

1条回答 默认 最新

  • donglie7778 2015-08-12 07:40
    关注

    You can define a constant and make use of __DIR__ magic constant.

    define('HEADER',__DIR__.'/');
    
    <link rel="stylesheet" type="text/css" href="<?php echo HEADER; ?>headerStyle.css">
    <script src="<?php echo HEADER; ?>headerScript.js"></script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?