I have a problem with my index.php together with my "include files". My index.php file is located inside the main folder named mysite and the rest inside it are subfolders with the respective .php files. The sub-folder files are working perfectly, except the index.php. The include files are messed up every time I preview it in the browser.
The path for the index.php file goes like this:
mysite/index.php
The path for the include files goes like this:
mysite/pages/include/header.php
Here is the HTML file with the include files:
<html>
<head>
<?php
include ("pages/include/headertop.php");
include ("pages/include/header.php");
include ("pages/include/nav.php");
?>
</head>
</html>
Kindly correct me if I have missed something here.
By the way, I'm using XAMPP.
Thank you and More power!
edit your index.php:
include ("../pages/include/headertop.php");
include ("../pages/include/header.php");
include ("../pages/include/nav.php");
and try again "mysite/index.php"
Firstly make sure that you are calling these include commands in a .php file and not .html file.
Secondly make sure that your server is up and running well.
After this try this code:
<html>
<head>
<?php
include "pages/include/headertop.php";
include "pages/include/header.php";
include "pages/include/nav.php";
?>
</head>
</html>
P.S: just remove the brackets from include.
Please also make sure that the file names are correct and they follow the proper case.
Related
I am refurbishing a website for a friend, e.g. making it more easy to program/maintain.
The server is running PHP 5.6 and in order to make life easier for me I wanted to uses php's include function to easily include stuff like the head or menu in every web page.
The file structure I use is index.php in the / directory and e.g. history.php in /pages. The files I am including e.g head.php lie in /php.
My problem is that in index.php
<?php include ('php/head.php'); ?>
perfectly executes and includes the designated file but in all sub directories such as /pages the same php code in history.php just doesn't execute at all leaving me with a blank line in the source code. I figured that this has to do with my PHP config or that said might be wrong, but I couldn't find the issue. I also tried calling the tech support of my web hosting provider but although they told me that everything should be working now I still get a beautiful blank line.
I've been searching for a solution to my problem for quite some days now, but I sadly haven't had any luck so far.
Hope the community can help
Thanks in advance
If you set the include_path to the full path then no matter in which sub-directory you have scripts they will always use the full path to the required file.
<?php
/* If you set this on every page the path will always be correct */
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/php/' );
include( 'functions.php' );
?>
<!doctype html>
<html>
<head>
<title>Include PHP files</title>
<?php
include( 'head.php' );
?>
<body>
<?php
include( 'menu.php' );
?>
<!--
regualar html,javascript etc
-->
<?php
include( 'footer.php' );
?>
</body>
</html>
Is it simply a file paths issue? If you're in a sub-map, then the path to head.php will be different:
<?php include ('../php/head.php'); ?>
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
I have looked around at a few different questions about the same sort of problem I'm having. I have took a solution and adapted it to my own project.
Here is my directory structure.
/css
-style.css
/includes
-shop.css
-header.php
-footer.php
/php
/js
/shop
-index.php
-index.php <-- homepage
-config.php
Inside my config.php I have
define('ROOT_PATH',$_SERVER['DOCUMENT_ROOT']);
My header.php
<?php include './config.php';?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo '<link href="'.ROOT_PATH.'/css/style.css" rel="stylesheet">';?>
<?php if ($_SERVER['REQUEST_URI'] == '/shop/'){echo '<link href="'.ROOT_PATH.'/includes/shop.css" rel="stylesheet">';} ?>
</head>
The only problem is, for any other page other than the root index.php file, the path for the config.php file becomes incorrect. Thus the CSS paths then become incorrect as ROOT_PATH isn't defined anywhere.
What would be the best way to handle paths when using includes?
Use $_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_ROOT'] gets the document root for eg var/com/images. $_SERVER['HTTP_HOST'] will get current url like http://example.com/images.TYour code should look like this
define('ROOT_PATH',$_SERVER['HTTP_HOST']);
And include this way
<?php include'../config.php';
Hope this helps you
Maybe using relative paths for the include in the other index.php file.
<?php include '../config.php'; ?>
You are using the server's actual filesystem path to refer to your stylesheets. That's like trying to do something like:
<link href="C:\your_website_path/includes/shop.css"...
and wont work.
I would recommend to change that to something like:
define('ROOT_PATH', 'http://www.your-website-url.com/');
Regards,
what you need to do is make the include for your config absolute, not relative. you begin the path with a dot ./config which means its relative. instead set up your header to include the config file with an absolute path like this
<?php include '/home/user/config.php';?>
This way, any page can find the file no matter its location in the directory structure.
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
I have a menu.php file I need included in each page on a site I am building. When I open the menu.php by itself it works fine. It's when I have it included in other files such as index.php then nothing loads.
Below is my index.php content. My menu.php is strictly html with a .css style sheet linked. I have searched and can find nothing to solve my problem. Any suggestions?
<html>
<head>
<!-- ... -->
</head>
<body>
<?php include 'menu.php'; ?>
</body>
</html>
EDIT: that is menu.php in the include, not header.php.
Let me try to explain how directories work:
ROOTFOLDER--
home.html
contact.html
PHPFOLDER---
header.php
CSS-----
stylesheet.css
if in home.html you have <?php include 'header.php';?> that wont work because the header is in the PHPFOLDER so you must <?php include 'folderphp/header.php';?>
Check your path, inclusion location etc.
For example, if a file located in a certain folder includes a file, that also includes another file.. the relative path is always based on the very original file opened.
What I do:
include $_SERVER['DOCUMENT_ROOT'].'/menu.php'; // if file is at /public_html/menu.php
or
include $_SERVER['DOCUMENT_ROOT'].'/includes/menu.php'; // if file is at /public_html/includes/menu.php
THat way no matter where you're opening from, you're calling an absolute path.