I do not know why this is not working. My header, footer, CSS and JS is not called when I am working in coffeeshop.php.
Mysite
I guess it is because it is not in the root folder. Therefore I made the include like this:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/vouzalis/resources/includes/header.php'; ?>
<?php include $_SERVER['DOCUMENT_ROOT'] . '/vouzalis/resources/includes/navbar.php'; ?>
But is still not working?
Here is where I put the full URL:
$_SERVER['DOCUMENT_ROOT'] is return folder path
http://$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"] it return server path
otherwise you can only use
<?php include '../resources/includes/header.php'; ?>
<?php include '../resources/includes/navbar.php'; ?>
try it
Related
I would like to include a file located in front/header/header.php from a front/user/login.php file.
How i can include header.php in login.php ? since it needs to step back from user folder and then enter to header folder.
i tried :
<?php include('/header/header.php'); ?>
<?php include('../header/header.php');?>
<?php include('../../header/header.php');?>
<?php include('header/./header.php');?>
<?php include('header/../header.php');?>
<?php include('front/header/./header.php');?>
<?php include('front/header/../header.php');?>
I think, you missed context of including path. Try to use:
include dirname(__FILE__) . '/../header/header.php';
It should helps.
I have a problem connecting my profile.php
main folder
index.php
page1.php
page2.php
page3.php
server folder
css folder
include folder
user folder
server folder contains
connect.php
css folder contains
stylesheet.css
include folder contains
header.php
footer.php
user folder contains
profile.php
all the file have
include ROOT.'include/header.php';
include ROOT.'include/footer.php';
inside my header.php
<!doctype HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
</head>
<body>
i used
chdir('maindirectory');
define('ROOT', getcwd().DS);
everything works except my profile.php cant seems to load my css file. The reason why iam separating profile into user because i am using htaccess to remove '.php' and i want to allow user to search through user/'username' as well.
if i never separate, i'll have problems going to other page like example.com/page2 it will search the user instead of going to page2.
Try to include the files using $_SERVER['DOCUMENT_ROOT'] instead of ROOT or define ROOT as $_SERVER['DOCUMENT_ROOT']
You don't need to use chdir and define to know working folder. You can use __DIR__ constant that returns working directory (see slash before include, __DIR__ don't return last slash):
include __DIR__ . '/include/header.php';
include __DIR__ . '/include/footer.php';
In profile.php __DIR__ have root/user value. This means that yo need to do:
include __DIR__ . '/../include/header.php';
include __DIR__ . '/../include/footer.php';
If your version of PHP don't support __DIR__, you can use dirname(__FILE__) instead:
include dirname(__FILE__) . '/include/header.php';
include dirname(__FILE__) . '/include/footer.php';
If you don't know or doubt the directory you are always can echo:
echo __DIR__;
in my root folder of my domain i have files header.php and footer.php, i have created a new folder and file in the new folder and wanted to include the header and footer files from main. So the paths are:
main folder: example.com/header.php
new file: example.com/folder/new_file.php <- in this file i want to include the header.
I have tried:
<?php include "header.php"; ?>
<?php include "/header.php"; ?>
<?php include "./header.php"; ?>
<?php include "../header.php"; ?>
and none of them work. Any help would be appreciated thanks.
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
it's will work .try may be its help in any of your files
include_once dirname(__DIR__).'/header.php';
This should work.
Thanks for all of your support. To answer my question thanks to all here are the answers.
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
and
php include "../header.php";
DO work and i was able to include a file from main directory so thanks to david and code360.
second part of why the css file was not being read is because i had it like this in header.php file:
<link rel="stylesheet" href="css/bootstrap.css">
and missed the first "/"
Thanks again to code360
I am currently using an index.php to include another file , it structure like:
root / index.php
/ new/ index.php
/ img/ test.jpg
And I write following in root/index.php :
<?php
require_once("new/index.php");
?>
the problem is , all paths in it are wrong. As all paths are based on new/index.php.
For example, In new/index.php my test.jpg is like
<img src="img/test.jpg" />
It shows http://www.test.com/new/img/test.jpg when I directly access new/index.php
But it shows http://www.test.com/img/test.jpg when I include the php in index.php.
How to fix it? I tried chdir() but it does not work expect Thanks
Make sure you always include with an absolute path, like:
require_once(dirname(__FILE__) . "/otherfile.php");
require_once(dirname(__FILE__) . "/../uponefolder.php");
require_once(dirname(__FILE__) . "/sub/folder/file.php");
Or use autoloading.
Did you just ask the same question twice? Use the required / included file as base directory in PHP
See my answer there.
The folder path you have in the require_once() function is relative to the directory your page is currently in. Have a look at the set_include_path function. Specifically, you could add that function to the top of your scripts to set the root include folder. Something like this:
set_include_path("/var/www/html/root");
require_once("new/index.php");
You could simply change the included HTML document base by adding a base tag.
https://developer.mozilla.org/fr/docs/Web/HTML/Element/base
<head>
<base href="your_new_url_base" target="_blank">
</head>
The HTML included file could check "Am I included from a parent?" if so "I have to change my base tag"...
// new/index.php
echo '<head>';
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) {
// called directly, no need base tag;
} else {
// included/required
echo '<base href="your_new_url_base" target="_blank">';
}
echo '</head>';
I want to know that how can i give include path of another folder.
For example, i have a project named test folder. In test folder has one folder which is assets folder and index.php file. In assets folder has two folder which are include and xyz folder. In include folder has two file which are header.php and footer.php.In header.php file has three link home, product and customer. In xyz folder has product.php and customer.php file.
In index.php, product.php and customer.php i want to include header.php and footer.php.
But i can't right path.
<?php include('../../header.php');?>
...........
<?php include('../../footer.php');?>
Please help me, how can i give right path which if i click product link then header.php and footer.php will be load.
Try this :
<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>
Ref: http://php.net/manual/en/function.dirname.php
If your Directory look something like this:
src
->includes
->header.php
->footer.php
web
->index.php
And you want to include the header and the footer in the index.php, do the following:
<?php
//index.php
include __DIR__.'/../src/includes/header.php';
echo "foobar";
include __DIR__.'/../src/includes/footer.php';
?>
http://php.net/manual/en/language.constants.predefined.php
Basically i have used like below
<?php include_once('../include/header.php');?>
<?php include_once('../include/footer.php');?>
echo realpath('../../header.php');
echo realpath('../../footer.php');
I think that if you can replace
../../header.php by ../include/header.php and
../../footer.php by ../include/footer.php.
then you will get accurate result.