PHP Include - full url - php

I've been using the php include function for my navbar for my website. It works well but....
My HTML says
<?php include 'include/navbar.html'; ?>
Now if I have a HTML page in /techpages/toptech.html so I would like to change my php include to <?php include 'http://example.com/include/navbar.html'; ?>. The problem is I get heaps of errors then.
Can someone help me?
Can someone answer one of these questions?
How can I make the PHP include function work with a full URL?
HTML has ../ to go into the parent folder. Is there a CSS equivilant?
Any help would be much appreciated.

Here are two approaches which I think will work for what I imagine you are trying to achieve:
You could include via include $_SERVER['DOCUMENT_ROOT'].'/include/navbar.html'; - this will always include the same file regardless of in what file in what directory you put the include
PHP has an include_path which specifies where to look for file-includes, you can add the /include directory in this include_path and from there on always include via include 'navbar.html'. But to be able to do this you have to have permission to access/modi the php.ini...

One of the options of doing that is:
include($_SERVER['DOCUMENT_ROOT']."/include/navbar.php");

Related

include() doesn't work when my page has GET param

I have a header.php file containing my DOCTYPE and all my links/scripts.
I use
<?php
// HTML DOCTYPE insert
include 'header.php';
?>
at top of all my pages to have only one header for everyone, and it works fine.
Now, I have another page that get from a database a summary of my products information. When someone click on the "read more" link:
<p>
read more...
</p>
another page opens with the full information displayed...
Actually that works...
BUT on my new page (display_product.php/id=[anynumber]) my included file doesn't work. So I have no nav bar, no scripts, no stylesheet. Only the text from my database.
AND the weird thing is that when I copy/paste the HTML of my generated display_product page and launch it on my browser, it works... O-o
So the generated code is good.
AND the second weird thing is that when I get rid of the /?id... my layout works fine (but I have no text anymore, of course)
Does one of you have an idea why this crazy things happens?
"Hi guys, Thanks very much Fred -ii this was it. it works perfectly. Thanks sergiodebcn for your concerne."
Since other answers were given and did not solve the actual problem, am posting my comment to an answer, in order to close the question.
Remove the slash from /?id
The slash is trying to instruct the server to probably find a folder after a filename, which technically looks like is what's happening here.
The ultimate solution for include and require functions with path issues, is to use the absolute filesystem path to the file that you want to include or require.
i.e you may say:
include("C:\\www\\app\\incs\\header.php");
Hint
To learn how to set the absolute path for include dynamically for your project, check the source code of two files of cakephp framework:
index.php
webroot/index.php

include files from subdirectory which calls from another directory

I have a file here: public_html/wiki/index.php
Im calling these files:
<?php
include "../auth.php";
include "../header.php";
?>
However, my page looks like this:
If I paste the same file in public_html/index.php then it will look like this:
I think I've narrowed it down to my header.php file which is being called from /wiki/ but header calls files from ../css.. and ../js and so on.
How can this be adressed? I've looked at many posts already which tells me to define a global variable but it doesn't help me. Like so:
<?php //config file in root folder
define("ROOT", __DIR__ ."/");
?>
and then calling them with this:
<?php
include_once("../config.php");
include (ROOT ."auth.php");
include (ROOT ."header.php");
?>
How should I do this so I can get all my javascripts and the actual site with me in another folder?
NOTE: I WANTED THIS AS A COMMENT BUT DO NOT HAVE THE REP
We have a different folder structure, but it is good practice to put your includes in one folder and from there link all the JS files (best practice is, place them in a pre-footer for speed) and then do
<?php include $_SERVER['DOCUMENT_ROOT'].'/includes/page-footer.php'; ?>
Just change the file name to suit you :) hope this helps
the problem was my include file didnt specify links as /js/file.js
i had them as js/file.js.
thanks #cd001

Why won't my function include work?

I'm trying to include other page from other folder. and i tried this and it won't show the "side.php"
<?php
include 'side.php';
?>
And the "side.php" has:
home
Well you have the answer to your question inside your question.
" Im trying to include other page from other folder"
It means that you have to go inside that folder in order to access the file
<?php
include 'folder_name/side.php'
?>
or just copy the side.php file where your working file is so that all files are in one place
Maybe I'm wrong because I don't know php so it might not work but you could check this out
Include PHP file into HTML file

php include results in page being written in pre

I'm learning php, been trying to make a cms, but when I use an include for the header this results in the page appearing as if it were written in pre tags. There's no errors when I debug or anything. And I'm completely stumped. When I put the header back in without the include it renders just fine.
<?php include("..\includes\layouts\header.php"); ?>
That's the include I'm using.
I've tried using the full path name, tried it in different browsers and using :
include($_SERVER["DOCUMENT_ROOT"]
Check that you are closing your php tags, ending your strings with semi colons etc.. My guess is that your html is being sucked into the php code which freaks out and just dumps it.
Maybe try one of the validators such as http://phpcodechecker.com (I have not used this one so I cannot comment on it's effectiveness)
Edit: I am rereading your post and I think I understand what you are trying to say - your header contains the path to your css and when you put it in a separate php file the css doesn't work? So the first thing to do is determine if the issue with the php path or the css path inside the header.php file. Look at your source code to see if the header code is being included - if it is then play around with the css path - though including the header in a php file will not cause that css path to change.
I am guessing that your header is not included at all from your mention of paths. include() works from the loading file's location. The path it wants is a server path and not a url. The one that you have above: ../includes... means that you have an include folder at the same level as the loading file such as (assume index.php is the main file):
/includes/layouts/header.php
/somedirectory/index.php
The ../ means - drop down one directory then go up from there.
If your path is more like:
/includes/layouts/header.php
/index.php
Then the include would be:
include('./includes/layouts/header.php');
Let me know if that works - if it doesn't try to explain your directory structure.

included files php do not appear in a browser

I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.

Categories