PHP include is not working? [duplicate] - php

This question already has an answer here:
Can't include or require an absolute path
(1 answer)
Closed 8 years ago.
I remember trying this plenty of times and it working but for some reason it isn't working for me now. I am using a local wamp server just to test this. It is not a public site. It isnt including the simple h1 tag that's located in the "/includes/header.php" for some reason.
My index.php code:
<html>
<head>
<title>Weekend Warrior Squad - Homepage</title>
<link href="styles/styles.css" rel="stylesheet">
</head>
<body>
<?php include("/includes/header.php"); ?>
</body>
</html>
My header.php code:
<header>
<h1>WWS - Weekend Warrior Squad</h1>
<p>
We are community based paintball team make the game of paintball
more enjoyable for ourselves and others.
</p>
</header>
All recommendations / answers are very much appreciated.

<?php
include "include/header.php";
?>
I think the reason for this is that you do not need the brackets. Try it and see if it works.

It's been a while since I've used wamp but I'm guessing the urls look something like localhost/project_name/index.php when you reference the include file as /includes/ it's actually looking for the file in localhost/includes/ and the file is not there.
Depending on where your file location is you can step out from it's folder and locate the includes folder.
include('../includes/');
This will go one folder up from where the file that is calling it. You can use .. as many times as you like to go to a new level, eg ../../includes

Try using
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
Or else you could use require instead of include.

Firstly, are you certain the relative path you set in your include is correct? Temporarily set it to an absolute path to be certain.
Secondly, you are missing the closing tag for your h1 in header.php, not sure whether this would affect your current issue but it could cause others.
Edit: I fail.

Related

PHP's include() function not working xampp [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 1 year ago.
Please bear with with me as I am new to web dev. I am attempting to use the include function in php to display a header on a web page.
I am using two very simple files, placed in the same directory and am using XAMPP.
index.php
<html>
<body>
<h1>Hello</h1>
<?php
include('header.php');
?>
</body>
</html>
header.php
<p>Hello</p>
When I open index.php, the statements from header.php never show up. All I see is one "Hello", as printed in index.php.
I have attempted to follow many tutorials online similar to this (ex https://www.w3schools.com/php/php_includes.asp, https://www.tutorialrepublic.com/php-tutorial/php-include-files.php), but none of them work for me.
Please let me know if you have any advice.
For security reasons you should use the following statements instead of include, unless you have a good reason to not do so:
Either
require_once 'header.php';
or
require 'header.php';
This will cause an E_ERROR in case the file was not found or you don't have permissions to include it. Otherwise you run into the risk not executing code which is mandatory for your program.

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.

The PHP include is not working

I have researched so much in blogs and forums and can not seem to find the solution to this problem. I am using the Wamp directory: C:\wamp\bin\apache\apache2.4.9\htdocs\lr
the lr at the end of the /htdocs is just the folder that I am holding my webpage in.
The code inside of my index.php :
<html>
<?php include('includes/head.php'); ?>
<body>
<?php include 'includes/header.php'; ?>
<div id="container">
<?php include 'includes/aside.php'; ?>
</div>
<footer>
© phpacademy.org 2011. All rights reserved.
</footer>
</body>
When I load the index.php in chrome or any browser the only thing that I am seeing is the footer which happens to be the only thing that is NOT set up in a php include tag.
Here is the code inside of my head.php
<head>
<title>Website Title</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/screen.css">
Very basic code which only gives me more confusion on why it is not working. If you would like more examples of code that I am using let me know. I will gladly provide some as I really need this to continue creating the database for my login/registration page(s).
It is likely that PHP does not find the files to be included.
Are you sure the folder 'includes' is in the same directory as your index.php?
Also, I would suggest to use "**/**includes/..." with a slash at the start to be sure that it searches the included files from the root directory, and not from the relative path of your index.php.
Besides, shouldn't your root be C:\wamp\www\ in WAMP?
You can check if the included file exist, this can give
you a clue to what is going on
if(file_exists('includes/head.php')):
include 'includes/head.php';
else:
echo 'file either not readible or does not exist';
endif;
Try to close head tag first in your head include file, if that doesn't work, I will dig deeper :D
EDIT:
Sometimes there are problems with file path. I usually use:
<?php include $_SERVER['DOCUMENT_ROOT']."/filename.php";?>
In Your case that probably be:
<?php include $_SERVER['DOCUMENT_ROOT']."/lr/includes/header.php";?>

Including a body file (that is much like a platform MySQL / PHP / JS) into a wordpress template

I’ve built an SEO platform that Works with MySQL database and outputs / Inputs to a PHP table.
I’ve made a template in wordpress that has the header and footer from the original theme, and left the body empty so I can call the application / platform.
<?php
/*
Template name: Keywords
*/
?>
<?php get_header(); ?>
<?php
include('/body_call/index.php');
?>
<?php get_footer(); ?>
I’ve tried to place the folder with all the files (body_call) in several places and called the index.php folder hoping that it fills the center of the template.
The index folder is something like this:
<html>
<div id="container">
<body id="body">
<?php include('body_call/body.php'); ?>
</body>
</div>
</html>
I know the problem is the calling part because the platform works well if it’s by its self…
And it's probably some worpress specific code that I'm missing.
Can anyone help?
Thanks,
Miguel
When you include a file from another file you are actually just including it in the file. That means you are actually in first file folder and not second. So if second file needs to include a file wich is stored somewhere else it has to be included by navigating from first file folder to it's actual path. It sounds a little tricky. So to nvigate to upper level if need you should use ../.
Anyway to avoid any confusion i would suggest to use absolute path so where ever the file is called from it will always be able to charge any file from anywhere.
Have a look at include documentation
an example could be
include(/home/user/body_call/index.php');

Parent directories and PHP

I really hope there's a simple solution to this.
<?php include("header.php"); ?>
Let's say I have a php header in my root folder simply titled header.php. In that header there is a link back to the home page, main.php, and main.php is also located on the root. No problem so far. Here's what header.php looks like. Simple stuff, right?
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="headerwrap">
<div id="linkbox">
<img src="images/mainlogo.png" />
</div><!-- End linkbox -->
</div>
However, let's say I have other pages in subdirectories. Subpage.php is located in a child directory of the root, and so it has to look back to the root to get the included header.php.
<?php include("../header.php"); ?>
That wouldn't be a big deal, except that header.php links back to main.php and also style sheets, none of which are in *subpage.php's directory, thus causing an error when someone on Subpage tries to get back to Main via the link in the header.
I'm just hoping there's a simple way to make this work, and that I don't have to copy and redirect all includes into every subdirectory. Also, there are too many pages to really reasonably include them all on the root. Sorry if this answer is posted elsewhere; I've looked and just have no real idea what I'm looking for. Thanks for your help. Hope all that makes sense.
You could just hard code main.php's path within header.php:
<img src="http://website.com/images/mainlogo.png" />
As opposed to a php prob this seems to be an html prob..
Your links should be relative links with a preceding / i.e.
Text
instead of
Text
how about using absolute links. header.php should also reference main.php absolutely, then there should be no troubles:
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php"); ?>
You can use the base html tag:
<base href="http://yoursite.com/" />
This way you can use that url as the base for all your links/stylesheets/images and you don't need to worry if they're in a subdirectory.
the best thing to do is to get in the habit of using
$_SERVER['DOCUMENT_ROOT']
that way you have no confusion as to what directory you're in, etc.
so including your header for example would be as simple as :
include $_SERVER['DOCUMENT_ROOT'] . "/header.php";

Categories