1

I am trying to include a PHP file in another directory.

Here is my file structure

settings\
    manage.php

website \
    index.php
    main.js
    main.css

For manage.php

echo 'Welcome to the manager! Here is your website:';
include('../website/index.php');

index.php

 <script src='main.js'></script>
 <link rel="stylesheet" type="text/css" href="main.css">

So, when I load manage.php, I do not get main.js or main.css. How can I get this to work? Maybe include is not the right way to go? I cannot modify anything in the website folder.

[I know iFraming is a possible solution but I'm hoping to get another answer]

6
  • Its usually best to use absolute urls vs relative urls. Commented May 30, 2013 at 1:09
  • The contents of the website folder are, from the user, I don't know what is going to be in there Commented May 30, 2013 at 1:13
  • 2
    I think framing is the best solution as you seem to be including a complete page / site in your manage.php file. That will probably lead to non-valid html (multiple html, body tags, etc.) and user styles overwriting yours (or the other way around) that will mess up your design anyway, even if you manage to include the correct assets. Commented May 30, 2013 at 1:30
  • 2
    yes, your manage.php real environment looks like frames are what they need and I think they're in quite rare cases. go for them. nobody will put third party html codes in their admin panel documents directly. Commented May 30, 2013 at 1:35
  • 1
    @DachiN. Even worse, php code. I did not even touch the security implications in my previous comment... Commented May 30, 2013 at 1:36

3 Answers 3

3

Since you cannot edit /website/ content you should could try this ugly code for a startup.

Add following just before include statement in your manage.php

echo('<base href="../website/">');

If it works for you, then you can think of sending a correct header with PHP before including a html file, instead of directly echoing a base tag.

Please consider comments of jeroen as down-to-earth solution and use frames

Sign up to request clarification or add additional context in comments.

5 Comments

+1 A cleaner solution would probably be via an .htaccess file if the OP has access to do that.
yes, and if not he can use ob_* methods to get whole /website/index.php via include (NOT OUTPUT) and then use Regexp to put <base href=.. code in an appropriate place and then output all.
True, but as I just commented below the question, it would seem framing is probably the way to go (depending on the exact scenario).
I have access to htaccess! This solution works well. What would be the htaccess solution?
Also, arent iFrames slow compared to this solution?
2

The problem here is that when the browser loads /settings/manage.php it will make requests for /settings/main.js and /settings/main.css which don't exist

You probably need to change your html in index.php to something like this:

<script src="/website/main.js"></script>
<link rel="stylesheet" type="text/css" href="/website/main.css">

Note I've made some assumptions about your URLs based on your directory layout so you may need to adjust my solution to make it work for you

1 Comment

he has no access to website/ directory
1

Based on the comment below the question: If you want to include some functions / code for your users (instead of the other way around; you including user's stuff in your code), you should look into the auto_prepend_file directive.

Basically, you specify in your php.ini file that you want to prepend (as a require) a certain php file before the main file.

Edit: As you don't have access to php.ini but you can use a .htaccess file, you can put this in your .htaccess:

php_value auto_prepend_file "/path/to/your/file.php"

3 Comments

Thanks for the reply but I don't have access to php.ini
@AA You can set these values in an .htaccess file as well, see my edit.
Thanks, I was wasting my time trying to make my own approach, ha ha. Thanks I got it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.