Jump to content


Photo

Why does my include file not work?


Best Answer trq, 03 May 2014 - 07:28 PM

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.

Go to the full post


  • Please log in to reply
15 replies to this topic

#1 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 06:10 AM

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.

 



#2 trq

trq

    Advanced Member

  • Administrators
  • 31,032 posts
  • LocationSydney, Australia.

Posted 03 May 2014 - 07:00 AM

So.. what is happening exactly?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#3 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 07:11 AM

My page is not loading any of the js / css files when trying to have them in the separate includes file. So I just get a regular html page.



#4 trq

trq

    Advanced Member

  • Administrators
  • 31,032 posts
  • LocationSydney, Australia.

Posted 03 May 2014 - 08:24 AM

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 ?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,618 posts

Posted 03 May 2014 - 08:56 AM

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.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#6 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 09:58 AM

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. 



#7 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 09:59 AM

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?



#8 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,618 posts

Posted 03 May 2014 - 10:05 AM

 

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.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#9 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 10:29 AM

Ok, so what am I doing wrong? I'm sorry but you guys are being very vague 



#10 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,421 posts

Posted 03 May 2014 - 11:05 AM

Tell us what you see when you right click > view source, Check to make sure the HTML output by your PHP code is correct (The PHP code should not be present).



#11 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 11:10 AM

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, 03 May 2014 - 11:11 AM.


#12 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,421 posts

Posted 03 May 2014 - 11:16 AM

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.



#13 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 11:24 AM

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, 03 May 2014 - 11:26 AM.


#14 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,421 posts

Posted 03 May 2014 - 11:46 AM

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, 03 May 2014 - 11:46 AM.


#15 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 321 posts

Posted 03 May 2014 - 11:55 AM

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.



#16 trq

trq

    Advanced Member

  • Administrators
  • 31,032 posts
  • LocationSydney, Australia.

Posted 03 May 2014 - 07:28 PM   Best Answer

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.


http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com