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__;
Related
Let says below are my web directories:
From web searching i get to use include or require_once and something similar.But I'm confuse in different directory causes much of directory error.
And my question is, how to include Navbar or Footer or Specific Pages in different directory.
Condition: Same directory as above;
For header,navBar and footer you should used simple
include(page/header.php);
About require and include function Reference
The require() function is identical to include(), except that it
handles errors differently. If an error occurs, the include() function
generates a warning, but the script will continue execution. The
require() generates a fatal error, and the script will stop.
But for image,css files and other js files you should follow these steps
Create config.php file
add this in config.php
define('BASE_URL', 'http://your_website_url');//OR define('BASE_URL', 'localhost')
you can use this path like
<?php
include('config.php');//add this code into top of the page
?>
and finally you can used where you want, like
For Style sheet
<link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
Create a config.php file in your home directory.
<?php
/* Saves the directory path to base directory as `BASE_DIR`
Example: /var/user1/home/public_html/
'Base directory' is where your homepage/index.php is located */
define('BASE_DIR', dirname(__FILE__) . '/');
?>
now just require this config.php file
<?php
require_once('config.php'); //add this line at top of your pages
// No need to include this inside 'header.php' and 'footer.php'
?>
Now u can include files easily, use it in index.php as follows:
<?php
require_once('config.php');
require(BASE_DIR . 'query/conn.php');
// becomes '/var/user1/home/public_html/query/conn.php'
include(BASE_DIR . 'pages/header.php');
include(BASE_DIR . 'pages/navbar.php');
include(BASE_DIR . 'pages/footer.php');
?>
Note: Similar approach is used by WordPress (learn more)
I have a question, I have a file in php where I need to include another one
<?php include('includefile.php'); ?>
but if I am in a subfolder I have to call this file like this
<?php include('../includefile.php'); ?>
however, some lines in this files will change, example
<a href="css/style.css">
now is
<a href="../css/style.css">
How can I do to set no matter where in the project
thanks
Generally, how this is achieved is by using a define which is named something like ROOT which stores the absolute path to the root of your project:
config.php
define('ROOT', __DIR__ . DIRECTORY_SEPARATOR);
Then in your other files, you include this one file (config.php) and make use of the ROOT define in your code. This saves you the hassle of having to worry about the ../ and how many directories up you need to go etc.
Reading Material
Predefined Constants
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
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 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>';