Include PHP file's JS, Css - php

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]

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

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

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"

Related

PHP Require method doesn't show any CSS style

I'm recently doing a website for a school project. In order to organize my work, I create a tree folder that keeps all the work organized. It is similar like this:
Back-Office
Pages
Home
home_test1.php
home_test2.php
home_test3.php
Login
Folder_Login
login.php
logout.php
Resources
CSS
style_home.css
style_navbar.css
style_footer.css
JS
script_home.css
script_navbar.css
Sections
navbar.php
footer.php
After all, with the require() method available in PHP, I want to call the "navbar.php" file to the "home_test1.php", "home_test2.php" and "home_test3.php", but the CSS style that is connected with the file "navbar.php" ("style_navbar.php"), doesn't display.
I've tried to change the path of the CSS style in the file "navbar.php" when I require() to the other file ("home_test1.php") and the CSS style shows up, but wont display in other file with a different path. How can I make this work dynamically? Sorry for long post and bad English grammar.
Thank you in advance.
You need to set your css and js files with absolute path instead of relative path
$dir = realpath($_SERVER["DOCUMENT_ROOT"]);
<link rel="stylesheet" href="<?php echo $dir.'/resources/css/style_home.css'; ?>" >
Without physically seeing you code it is quite hard to debug however there is an "obvious" answer that I'll suggest as a starting point.
The important thing to remember is that PHP and HTML are processed in completely different places. PHP executes on the server and should be used to build a full HTML "document" which it gives to the client/browser. The client/browser then reads the document provided and renders it according to HTML standards.
Calling require() will tell PHP to get the file and slot its contents directly where it was called and as it is a CSS file it will need to sit within the style tags. With a lot of modern browsers, if you use require on a file outside of the html tags, the content will be dumped at the top of the screen or simply ignored due to invalid syntax.
Alternatively if you would like to simply use tell the browser to include the CSS file, you could use the good old method of using <link rel="stylesheet" href="/path/to/file">. It's good to know when and when not to use PHP.
PS: You have .css files in your JS directory.
In PHP, there is a global variable containing various details related to the server. It's called $_SERVER. It contains also the root:-
$_SERVER['DOCUMENT_ROOT']
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
<link rel="stylesheet" href="<?php echo $path.= '/Resources/CSS/style_navbar.css';?>" />
?>

Why won't this PHP code feed in to the link TAG href attribute? (local host, not using laravel)

Disclaimer: I haven't got a clue what I'm doing with PHP I'm just playing around with it.
I have my css file in a folder named CSS and then my header.php and footer.php in the main site folder. If i include the header.php in other directories I am just using:
<?php include('../header.php'); ?>
I know this isn't the way to do it however I don't know how to configure it probably (with a config.php file etc..) but my issue is, once the header's included in files in any directory of course it will look for the css/main.css file in that folder so I've tried doing the following:
<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']. '/JAGS/css/main.css' ?>" />
When I use the php line in the body it displays the path
E:/xampp/htdocs/JAGS/css/main.css
but if I use it there in the link tag then doesn't work.
What seems to be my problem other than the fact I'm clueless with PHP. Is there something else I should be using? Is there something I need to do in my xampp config files?
Edit: By "doesn't work" I mean the styles are not being applied.
Edit 2: Inspecting shows the following:
<link rel="stylesheet" href="E:/xamppp/htdocs/JAGS/css/main.css">
I know there's an extra p on the end of xampp, this is actually what I have the folder named. Is it because it's not saying "localhost/JAGS/CSS/main.css"? If so what would be the reason for this?
Edit 3: Console shows error below:
Not allowed to load local resource: file:///E:/xamppp/htdocs/JAGS/css/main.css
Edit 4: Not using Laravel
Thank you
Why do you require the document root, you should just place a dot in front of it and set the base url in a meta tag.
<base href="yourdomain.com">
<link rel="stylesheet" href="./JAGS/css/main.css'/>

Relative URL issue

I will have multiple folders/modules to access common files. But accessing them seems to be big deal for me!
I did gone through this link to understand the relative positioning and managed to solve some . But not all. Reference: Relative URL's/paths in php
My folder structure is as below:
Website runs on root folder:
/(index|ajax).php
and then the subfolders:
/css/style.css
/img/*.(jpg|png|gif)
/inc/(header|footer).php
/js/*.js
/registration/(ajax|getsubjects|response|success).php
Now, this is how I included files in the index.php page(this displays correctly, meaning, style,css,js,config all accessible)
<?php
include('inc/header.php');
?>
content here
<?php
include('inc/footer.php');
?>
This index page will have to fetch getsubjects.php, response.php and then finally land in success.php.
The success.php need some styling whereas the previous two were only for processing.
So now in the success.php I access header and footer as below:
include('../inc/header.php');
include('../inc/footer.php');
But this doesn't apply any styling!
inside header.php and footer I include files like this:
<link rel="stylesheet" href="./css/style.css">
<script src="./js/script.js"></script>
How should I include the files here please?
./css/style.css means from current directory and would achieve the same result as css/style.css. The easiest answer is to determine what the base path of your application is and use that. For instance, if your application is running as http://myapp.com, then you could set all your front-end paths to /css/style.css. If your app runs in a subdirectory, such as http://example.com/myapp, then your paths would be /myapp/css/style.css.
This does not apply the same on the PHP side. For them, you should really use document-relative paths. Having a PHP file that you include in multiple places in your app, the contents of which having something like include('../myDoc.php');, can lead to complications as the path isn't based on the included document's path, but rather the including. So using document-relative paths, you get around this include(__DIR__ . '/../myDoc.php');. Just something to consider if your app grows.
Your PHP-includes seem to be correct. But in your HTML you need to change the linking to the CSS and JS Files (maybe even to your images).
You could use absolute paths:
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
the leading dot makes your paths relative to the HTML-Document, so if they are linked from a document in a subfolder, they point to a wrong location.
Including files with
<?php
include("page1.php")
?>
put the code (or content) from page1 into the caller page.
So you may have to detect from where your pages are called, or try absolute links (beginning by /)
I hope I answer you question correctly.

Ways to resources in PHP

I try to resolve my problem with resource path in Php. In file header.php I include scripts and stylesheets, but when I require_once('../templates/header.php'); in file login.php from views directory I get 404 error code on all my resources, because all files must be in subdirectory of views direcroty. How I can solve this problem?
Path:
/var/www/reg/templates/header.php - path to header
/var/www/reg/views/login.php - path to login
/var/www/reg/js/script.js - path to js
/var/www/reg/css/style.css - path to css
according to your file structure
inside header.php
try this way:
<link href="/reg/css/style.css" rel="stylesheet" />
<script src="/reg/js/script.js" ></script>
As I have tested it works for me.
This is how I'd probably do it, not sure if best practice, but similar to the way wordpress works, IE
In your a index file (in the reg/ dir or in a config file if you have one):
define('SITEPATH', dirname(__FILE__).'/');
Then you can require files like this:
require_once(SITEPATH . 'templates/header.php');
Then when you're calling your resource files, you can do something similar this:
<script src="<?php echo SITEPATH; ?>js/script.js"></script>
But, imo, backend stuff shouldn't really be on template files, but because your directories are different to how I'd lay them out.

PHP Include Paths

I'm new to PHP and I'm having a problem when trying to link my CSS files using include.
Basically I need my files to link back to a certain directory no matter how far down the file is. I have tried using
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/sysprogs/required/header.html';
?>
But header.html contains the links to my css files so the directory it ends up looking for the css files in is
http://localhost/SysProgs/software/CSS/style.css
instead of where I want it to go to which is
http://localhost/SysProgs/required/CSS/style.css
I hope that made sense and I hope you can help me
Thankyou for all your help everyone!
I would definitely not use <base>. I've run into many problems with this before. If you use <base>, ALL of your links will become relative to that base value.
Instead, I would recommend setting PHP constants for common directories. For example:
PHP Code:
<?php
define('CSS_DIR', '/SysProgs/required/CSS/');
?>
HTML Code:
<link href="<?php echo CSS_DIR ?>style.css" rel="stylesheet" type="text/css" />
One Idea
Use the full URL in header.html. This will be unambiguous and robust.
<head>
<link href="/FULL_BASE_URL/style/style.css" rel="stylesheet" type="text/css" />
</head>
Another Idea
Use the <base> header tag. This allows you to specify a base URL for links, including CSS, and may require the least work in the short term (see note below).
<head>
<base href="FULL_BASE_URL" />
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
More at w3schools
Note: As is noted in the comments below base may ultimately cause more confusion than it is worth.
I like to define both an absolute path and a webroot in a central place in your application:
<?php
define("APP_WEBROOT", "/myapp");
define("APP_ROOTDIR", "/home/www/example.com/htdocs/myapp");
?>
you can then "absolutize" the correct links like so:
<?php echo APP_WEBROOT; ?>/software/CSS/style.css
I prefer this
over <base> because that tag creates confusion and makes code harder to maintain in the long run
over using absolute paths /software/CSS/style.css because those make you unable to install your application in a subdirectory of your choice: You will always be bound to the root directory.
I run into this problem a lot when designing sites. When I have custom CMS, I use the following:
$basedir = "root_directory/";
$basedirPHP = $_SERVER['DOCUMENT_ROOT'].$basedir;
$basedirHTML = "http://".$_SERVER['SERVER_NAME'].$basedir;
I define $basedir so I can move the site to different subdirectories in the server without any effort. The $basedirPHP and $basedirHTML are so I can call on files either with php, or like you mentioned, when linking CSS, JS, etc.
If you're on wordpress, just use the good ol' bloginfo('template_directory'); to do the same in template files.
The first thing for you to understand, is your question has nothing PHP related. It is as simple as just filenames in your HTML questuon. Without PHP it will remain the same. Just use absolute path to your CSS file
And another thing to think of: consider to accept some questions.

Categories