Jump to content

Using PHP include to include a php file with more includes!


Molson31

Recommended Posts

I know what my problem is, but I'm not sure how to go about it. I am trying to include a php file with many includes (js, css, php) in another php file in a higher directory. I am using apache and run off localhost (this is an internal website).

 

I have my main index

/htdocs/index.php

and the file I want to include.

/htdocs/tables/php/projects.php

 

When I include /php/projects.php in my index.php, as expected, all my links are broken to my js and css files, and even further nested php files (within js files). I believe I am supposed to DEFINE some kind of ROOT or DIR or something, but I am not having success Googling these things. 

 

Here is my code to look at:

Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BLST</title>
</head>

<body>
    <?php $projectsPage = "/tables/php/projects.php"?>
    <li><a href="index.php?page=<?php echo $projectsPage;?>">Projects</a></li>
    <?php if(is_null($_GET["page"])) { 
                $page = ""; 
        }else{ 
            $page = $_GET["page"]; 
        } 
       include($page); 
    ?>
</body>
</html>

projects.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <title>Projects</title>

        <style type="text/css">
            @import "css/test.css";
            @import "http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.23/themes/smoothness/jquery-ui.css";
        </style>

        <script type="text/javascript" language="javascript" charset="utf-8" src="js/jquery.min.js"></script>
    </head>

The error being that any css, js or php file in projects.php is not found, as the browser reports it is looking for these files from the main root. I want to be able to alter this without trying to add something to projects.php.

Edited by Molson31
Link to comment
Share on other sites

when you include a file using php, the contents of that file is essentially copy/pasted at the point of the include() statement. it becomes part of the main file's code and all the markup in it is part of the main file and is relative to the main file.

 

you can/should be able to use the magic constant  __DIR__ (and strip off the leading $_SERVER['DOCUMENT_ROOT'] part) in the code in the projects.php file to build the relative directory path from the main file, to produce a relative url that the browser can use to request the js/css... files.

 

however, the projects.php code is a complete html document in itself. when you include that into your main file, you now have a broken html document. the end result on the server-side must be a valid html document.

 

edit: you also have an issue with your include() statement. the include statement expects a file system path. in the value you are supplying to it - "/tables/php/projects.php", the leading / will be treated as the root of the current disk. the file system path you supply to the include() statement should either not have the leading slash (making it relative to index.php) or it should have $_SERVER['DOCUMENT_ROOT'] prepended to it to make it an absolute file system path.

 

the gist of this is, you have file system paths that php is using and you have urls that the browser is using and they must all be correct in the context of where they are being used.

 

you also need to validate $_GET['page'] to insure someone doesn't supply ANY PATH they want and obtain access to protected content and actions.

Edited by mac_gyver
Link to comment
Share on other sites

Thanks for the reply. I was hoping to not change anything in project.php because there are tons of internal links all over the place in the js and php files and it would be a nightmare to add filepaths to them. I was hoping that I could have changed something in the index.php.

 

Alas I will simply do this another way.

Link to comment
Share on other sites

It looks like your link is wrong. You said your directory for the second file is here: /htdocs/tables/php/projects.php, but you're including a file at php/projects.php. You need to send it to the tables directory first, then into the php directory, and finally to your document.

 

Oh, my bad...I looked more closely at it. I think your link is right. Sorry.

Edited by nodirtyrockstar
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.