Proper way to include header and functions files in PHP - php

I have my all files in public_html directory like this
global_assets
assets/layouts/header.php
assets/setup/env.php
order/index.php
order/info/index.php
Now all is working fine in order/index.php in which I am including header like
include '../assets/layouts/header.php';
Now I have tried include header in my order/info/index.php like below
include '../../assets/layouts/header.php';
Header getting included properly in this too, but its giving me error called
PHP Fatal error: require(): Failed opening required '../assets/setup/env.php' in /home/myusername/public_html/assets/layouts/header.php on line 7
I will have many similar errors because of directory structures. I think I am not following proper way to include files so I can call it properly from any directory, sub directories without any issues. Let me know if anyone here can help me for it.
Thanks a lot!

You can define and retrieve the root of your project in several ways, then you can always access php files from this root.
environment variable
include getenv('MY_PROJ_ROOT').'/assets/...';
auto-prepend-file
//php.ini
auto_prepend_file="/path/to/prepend-file.php"
//prepend-file.php
define('MY_PROJ_ROOT', '/path/to/project-root');
//any other files
include MY_PROJ_ROOT.'/assets/...';
include-path
//php.ini
include-path=".:/path/to/project-root"
//other files
include 'assets/...';

Related

Failed opening required file from other file

So I have domain.be/index.php
Then I have require Controllers/indexController.php;
In that file I have require ../Model/opalus.php;
So I builded the first controller, for my framework which worked, yèeey.
But If I use my controller in the index.php I get
PHP Fatal error: require(): Failed opening required '../Model/opalus.php' (include_path='.:/opt/cpanel/ea-php56/root/usr/share/pear') in /home/webkeuke/public_html/Controllers/indexController.php on line 2
I have tried using _DIR__ no ../ or ./ etc, can somebody please fix this?
Sometimes multiply file inclusions by relative Paths (../, ./) in PHP on complex projects can make a lot of problems especially in error tracing.
To prevent your project and yourself to stuck in this problem I recommend to do all file inclusions with fullpaths (CHANGE include("../inc/extra.php" - TO include("/var/www/myproject/inc/extra.php").
But this method can also be make a lot of work if your filesystem paths changes (f.e. when you change your Server environment or the directory then you have to change all files with an include in it).
So the best solution is to define the basic filesystem path in a global variable at a point where your scripting starts. Then use the defined basic filesystem path variable on every include();
Example for your case (index.php):
<?php
define ("FSPATH","/home/webkeuke/public_html/");
[your code]
// include /home/webkeuke/public_html/Controllers/indexController.php
include (FSPATH."Controllers/indexController.php");
[your code]
?>
Example for your case (Controllers/indexController.php):
<?php
[your code]
// include /home/webkeuke/public_html/Model/opalus.php
include (FSPATH."Model/opalus.php");
[your code]
?>
Ths solution should prevent you to get stucked at a point with wrong include Paths.

PHP include file behaves strange after a JQuery call

It's been a while since I came across with this site. So far I've been viewing questions asked by different users and using the tips handed by the commenters in order to solve my issues and code problems. Sadly I couldn't manage to find a relative post to my current issue, hence I'm posting my very first help thread.
I'm using and 'include.php' file in my application which includes all the relevant files to my project. The file is located in the sub directory 'includes', therefore its path is 'includes/include.php'. Everything worked fine since I have started codding until my last decision to start using JQuery in my codes. Since I'm pretty new to both of these languages, I came across an annoying problem when trying to load a certain PHP file into one of my HTML divs. Instead of spending my time by verbally explaining to you what the problem is, I'll simply go ahead and show it to you.
In the top of my PHP files I use the next code in order to include my main include file:
require_once "/includes/include.php";
The content of my 'include' file is simply including the rest if the files, it goes like this:
require_once 'user.php';
require_once 'db.php';
Everything works fine with these files. Now I have a different include file, a script file, that I use in the middle of my code to post a certain HTML script. I use the next code in order to load it in my main PHP file:
<?php include '/includes/script_files/loadAdminMessageClass.php'; ?>
Everything works just fine so far as well. The problem starts when I try to refresh a certain div using JQuery and re-load the same 'loadAdminMessageClass' file to that div. This is the code line I use in order to reload the div:
$('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
Here starts the problem. From this point onwards, the line 'require_once '/includes/include.php' in the first 'loadAdminMessageClass' doesn't work. The next error comes up in my div after the reload process:
Warning: require_once(/includes/include.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\includes\script_files\loadAdminMessageClass.php on line 8
Fatal error: require_once() [function.require]: Failed opening required '/includes/include.php' (include_path='.;C:\php5\pear') in C:\wamp\www\includes\script_files\loadAdminMessageClass.php on line 8
I've been searching this problem for ages and nothing was like the answer I was seeking for. Seems like when I call the 'loadAdminMessageClass.php' file from JQuery, it won't treat the 'require_once' call the way it should.
--
I'd be glad to have a response to this issue of mine. Thanks.
I'm actually surprised this line works:
require_once "/includes/include.php";
This refers to the root of your server's file system. You're mistakenly assuming PHP respects the document root of your website when constructing include paths.
You have to explicitly retrieve $_SERVER['DOCUMENT_ROOT'] to find out where the website is running (something like /var/www/mywebsite or /home/user/public_html usually), and then construct the paths from there:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
require_once ROOT_PATH.'/includes/include.php';
You should try this one
require_once dirname(__FILE__) . '/includes/include.php';

php include with wamp

I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.
So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;
$_SITELOC = "localhost/eee/";
And then each time i want to include a file i will go;
include "$_SITELOC/scripts/inc/_header.php";
But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;
Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
I read somewhere that it might be to do with the include path so i tried;
set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");
but this too did not work and now i'm not sure where to go.
Thanks, Chris
localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/. When you move to web server, you get the public address http://www.eee.lo/.
When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\ on windows, then your include path should start with D:\www\eee\.
So, basically you have to use two variables to keep paths.
$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\"; //For all internal file includes.
In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.
Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, i.e. like
define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, i.e.
define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, i.e. file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.
If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source. It seems like you actually want to include by local file system path.

PHP is looking for a file in the wrong place

I am an amateur web developer and I am trying to get my site live for the first time. The site is working and all of the files are uploaded but the site is not loading my PHP includes.
Here is the error message:
Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in /home4/myUsername/public_html/index.php on line 3
How can I get PHP to look in public_html/ rather than public_html/index.php?
EDIT: I have tried editing the include path. It doesn't seem to change where php is looking for the file. Additionally my includes work properly in localhost.
I'm going to assume this is your folder structure:
public_html/index.php
public_html/includes/header.php
Generally (not always), $_SERVER['DOCUMENT_ROOT'] will now reflect the path to the base public_html directory (this I'm assuming based on the context of your message). This means you can always point to the root this way. - no matter if you have /index.php or /my/deep/file/structure.php
Try this with your include statement on index.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
You may need to change the include path in your php.ini file or use set_include_path() to change the include path.
Here is the manual entry for the function call if you'd like to read more about it.
Have you checked already the include file?
in given. include(folder_includes/file_header.php);

Web application not finding file in what appears to be the correct path

I tried to reorganise my facebook application files into better folders and now when I try to load my application on facebook it throws the following error:
Warning: require_once(../scripts/mysql_connect.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '../scripts/mysql_connect.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
The second error is a result of the failure of the first one I think. The weird thing is that my program works perfectly fine when I'm accessing through the localhost url in the browser but when I run it through facebook it throws this error.
In my root folder I have a "Model" folder where the Database.php file is kept and a "scripts" folder where the mysql_connect.php file is kept. I have tried clearing my cache on the browser and using private browsing but it is still throwing this error.
Does anyone know what could be wrong?
EDIT: Sorry this does not seem to be working in localhost either now! One thing I think is weird is that it says "failed to open stream : NO SUCH FILE OR DIRECTORY IN" and then lists the database.php path. Why is it looking in there?
I use the line require_once('../scripts/mysql_connect.php'); at the top of my Database.php file to include it.
It seems that on the server there is a different working directory. Something like the following owuld be a quick fix
dirname(__FILE__) . '/../scripts/mysql_connect.php'
Ok I've fixed this, but I'm still unsure why it was a problem. In my root folder I have two folders: scripts, and Model. I have a database.php file in my Model folder which has the following line of code in:
require_once('../scripts/mysql_connect.php');
For some reason this did not work, but by replacing the line with:
require_once('/scripts/mysql_connect.php');
This started working again! Just thought I would post the solution. Does anyone know why the other way failed? I thought the ".." just makes it go up one level in the folder hierarchy i.e. to the root folder in this case, and then follow the path which would have taken it to the file...
EDIT: This appears to only have solved it for localhost and not through Facebook...
Do you have permission to edit your php.ini file? One solution would be to find the line with the include path (you'll notice it has C:\xampp\php\PEAR in there already). Include the full path to the "scripts" folder you're referencing, then you can change the php code to read:
require_once('mysql_connect.php');
PHP will be able to find this file since it's in the include path. Just make sure that if you start adding several include paths that you shouldn't have a file named mysql_connect.php in two or more of the different include paths because you'll most likely get errors. Hope this helps. It definitely makes for some cleaner code.

Categories