Jump to content

Why does my include file not work?


SalientAnimal
Go to solution Solved by trq,

Recommended Posts

Hi all,

 

I don't understand why my includes file ins't working. When I add the files inside my includes file to the page it all works as expected. I then try moving the CSS & JS files out of the main file and into and includes file that is located in my includes directory. Then it stops working. Any guidance here?

 

Directory structure is:

Main Directory = htdocs (Houses my main file)

Includes Directory = includes (Houses css_includes.php & js_includes.php)

JS Directory = js (Houses all the mentioned *.js files)

CSS Directory = css (Houses all the mentioned *.css files)

 

This is how I am including it in my main file:

<?php
include_once 'includes/css_includes.php';
include_once 'includes/js_includes.php';
?>

Inside each directory to have:

css_includes.php


<link type="text/css" rel="css/stylesheet" href="../css/reset.css">
<link type="text/css" rel="css/stylesheet" href="../css/metallic.css">
<link type="text/css" rel="css/stylesheet" href="../css/style.css">
<link type="text/css" rel="css/stylesheet" href="../css/shCoreDefault.css">

js_includes.php

        <script type="text/javascript" src="../js/XRegExp.js"></script>
        <script type="text/javascript" src="../js/shCore.js"></script>
        <script type="text/javascript" src="../js/shLegacy.js"></script>
        <script type="text/javascript" src="../js/shBrushJScript.js"></script>
        <script type="text/javascript" src="../js/shBrushXML.js"></script>
        <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="../js/zebra_datepicker.js"></script>
        <script type="text/javascript" src="../js/core.js"></script> 

When adding these to the main file I just remove the " ../ " in front of each of the src / href and it works perfectly.

 

Link to comment
Share on other sites

Does...

 

<link type="text/css" rel="css/stylesheet" href="../css/reset.css">
<link type="text/css" rel="css/stylesheet" href="../css/metallic.css">
<link type="text/css" rel="css/stylesheet" href="../css/style.css">
<link type="text/css" rel="css/stylesheet" href="../css/shCoreDefault.css">
Appear in your source ?
Link to comment
Share on other sites

when you use php to include a file, it essentially replaces the include() statement with the content of the file being included. it doesn't matter where the file being included is stored at, it could be twenty folders deep in your site or even below/outside your document root folder, any links in the resulting web page are relative to resulting web page, they aren't relative to where the included file is stored at.

Link to comment
Share on other sites

Does...

 

<link type="text/css" rel="css/stylesheet" href="../css/reset.css">
<link type="text/css" rel="css/stylesheet" href="../css/metallic.css">
<link type="text/css" rel="css/stylesheet" href="../css/style.css">
<link type="text/css" rel="css/stylesheet" href="../css/shCoreDefault.css">
Appear in your source ?

 

No, its in my includes files. When it was in the source less the ../ it worked fine. 

 

But when I moved to an includes directory, I had to add the ../ to browse more folders down. 

Link to comment
Share on other sites

when you use php to include a file, it essentially replaces the include() statement with the content of the file being included. it doesn't matter where the file being included is stored at, it could be twenty folders deep in your site or even below/outside your document root folder, any links in the resulting web page are relative to resulting web page, they aren't relative to where the included file is stored at.

Not sure what you mean here?

Link to comment
Share on other sites

 

But when I moved to an includes directory, I had to add the ../ to browse more folders down. 

 

no you didn't, because it's not the browser that's including the file, it's php.

 

the source that trq referred to is the 'view source' in your browser. the resulting html/css/javascrpt that's output to the browser must be correct.

Link to comment
Share on other sites

This is what I see when I go to view source:

<!doctype html>
<html>

    <head>

        <meta charset="utf-8">

        <title></title>

<script src='../js/XRegExp.js' type='text/javascript'></script>
<script src='../js/shCore.js' type='text/javascript'></script>
<script src='../js/shLegacy.js' type='text/javascript'></script>
<script src='../js/shBrushJScript.js' type='text/javascript'></script>
<script src='../js/shBrushXML.js' type='text/javascript'></script>

   
<script type='text/javascript'>
            SyntaxHighlighter.defaults['toolbar'] = false;
            SyntaxHighlighter.all();
        </script>       
        
         
        <script src='../js/jquery-1.10.2.js' type='text/javascript'></script>
        <script src='../js/zebra_datepicker.js' type='text/javascript'></script>
        <script src='../js/core.js' type='text/javascript'></script> 
        <script src='../js/my_location.js' type='text/javascript'></script>       
        <link type='text/css' rel='css/stylesheet' href='css/reset.css'><link type='text/css' rel='css/stylesheet' href='css/metallic.css'><link type='text/css' rel='css/stylesheet' href='css/style.css'><link type='text/css' rel='css/stylesheet' href='css/shCoreDefault.css'><body></body>
    </head>

That's only the top section of the code, up until the head closing tag

Edited by SalientAnimal
Link to comment
Share on other sites

Okay so is the file paths correct to your JavaScript and CSS files?

 

Remember the file paths used in the HTML must be relative to your sites (current) url. Where as the file paths used in PHP are relative to where the script is located on the file system.

Link to comment
Share on other sites

Ok, so the directory structure i have is a follows... Just to see if maybe I am missing something.

 

htdocs is the main directory as it is by default. Inside this directory I have three directories

 

includes

js

css

 

inside the includes directory I have my two include files (the include files contain the code as show through the "view source code"):

  • css_includes.php
  • js_includes.php
  • for testing my main file (test.php) is in this directory as well, however I want it to eventually be directly in htdocs

Inside the js directory I have all the js files that are being shown in the source

Inside the css directory I have all the css files that are being shown in the source

 

If I add the file location directly into my file (test.php) without using the includes it works perfectly. However i want to keep them out of the file itself so that in future I can just include all the required scripts with a single include.

 

Hope this makes sense?

Edited by SalientAnimal
Link to comment
Share on other sites

The problem then is most likely the .. at the start of the file path in your HTML. Just start your HTML file paths with a / instead

css_includes.php

<link type="text/css" rel="css/stylesheet" href="/css/reset.css">
...etc
<link type="text/css" rel="css/stylesheet" href="/css/shCoreDefault.css">

js_includes.php

<script type="text/javascript" src="/js/XRegExp.js"></script>
...etc
<script type="text/javascript" src="/js/core.js"></script>
Edited by Ch0cu3r
Link to comment
Share on other sites

Thanks, I managed to get it to work. I had single quotes instead of double quotes around the sources etc... To be honest I didn't know this made a difference.

 

I also removed the .. in front of each file just for safe keeping and it seems to be working. It just appears as if not all the css and js files are loading, which I struggle to understand.

 

Thanks for the assistance.

Link to comment
Share on other sites

  • Solution

 

 

Thanks, I managed to get it to work. I had single quotes instead of double quotes around the sources etc... To be honest I didn't know this made a difference.

 

It doesn't.

 

 

I also removed the .. in front of each file just for safe keeping and it seems to be working.

 

That indeed would have been the cause of your issue.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.