High schooler
High schooler

Reputation: 1680

Include PHP file's JS, Css

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]

Upvotes: 1

Views: 742

Answers (3)

jeroen
jeroen

Reputation: 91734

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"

Upvotes: 1

Davit
Davit

Reputation: 1373

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

Upvotes: 3

joshuahealy
joshuahealy

Reputation: 3569

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

Upvotes: 2

Related Questions